整数除法:
1、整数类型的除法运算总是会截断余数。
2、用一个值为0的变量做除数将产生一个运行时错误。
整数溢出:
1、整数类型在运行算数运算时可能会溢出。默认情况下,溢出默默地发生而不抛出异常。
例:int a = int.MinValue; a--; 则 a == int.MaxValue;
整数运算溢出检查符:Checked
1、Checked运算符的作用是在运行时当整型表达式或语句达到这个类型的算术限制时,产生一个异常而不是默认失败。
例:int a = 20; int b = 20; int c = Checked(a*b); // 只检查一个语句
Checked{ int a= 20; int b = 30; int c = a*b; int d = a/b;} // 检查整个程序块中的语句
常量表达式的溢出检查:
无论是否开启Checked编译器开关,当有常量在表达式中时都会检查是否溢出。除非使用UnChecked
例:int x = int.MinValue + 1; // 编译时错误
int y = unchecked(int.MinValue + 1); // 没有错误