//强制转换 + 有无溢出检测之----(类型)转换方式 及 Conver.方式
//如果用“(类型)”方式进行类型转换,要检测溢出用checked或unchecked
using System;
namespace conversionN
{
class conversion
{
static void Main()
{
Console.WriteLine("int 的最大值" + int.MaxValue);
Console.WriteLine("long 的最大值" + long.MaxValue);
int a;
long b = 1234567891111;
a = (int)b;//强制转换,无溢出检测,会产生致命的错误
Console.WriteLine(a);//结果可以输出但输出变成了1912277159
int c;
long d = 1234567891111;
c = checked((int)d);//强制转换,进行了溢出检测,会报错,防止了程序的致命错误
Console.WriteLine(c);
int e;
long f = 1234567891111;
e = Convert.ToInt32(f);//使用Conver方式,会自动进行溢出检测,防止了程序的致命错误
Console.WriteLine(e);
}
}
}

浙公网安备 33010602011771号