checked 和 unchecked关键字用来限定检查或者不检查数学运算溢出的;若是使用了checked发生数学运算溢出时会抛出OverflowException;若是使用了unchecked则不会检查溢出,算错了也不会报错。html
1. 一段编译没经过的代码算法
1
|
int
a =
int
.MaxValue * 2;
|
以上代码段编译没有经过,在VS2010中会有一条红色的波浪线指出这段代码有问题:”The operation overflows at compile time in checked mode”。这说明了编译器会在编译时检查数学运算是否溢出。可是编译时能检查出溢出的状况仅限于使用常量的运算。2中的代码编译器就不报不出错误来了。ide
2. 一段编译经过可是不能获得正确结果的代码函数
1
2
3
|
int
temp =
int
.MaxValue;
int
a = temp * 2;
Console.Write(a);
|
我先把常量int.MaxValue的值给了临时变量temp,而后使用临时变量乘以2计算结果赋值给a;这段代码是能够正常执行的,执行结果将输出 -2。测试
这说明在运行时默认状况程序是不会检查算术运算是否溢出的,cpu只管算,对于它来说按规则算就是了,结果对不对不是他的错。spa
正常执行了,而结果是错误的,这是很是危险的状况,该如何避免这种危险呢?请看3code
3. 使用checked关键字,溢出时报警htm
1
2
3
4
5
6
7
8
9
10
|
int
temp =
int
.MaxValue;
try
{
int
a =
checked
(temp * 2);
Console.WriteLine(a);
}
catch
(OverflowException)
{
Console.WriteLine(
"溢出了,要处理哟"
);
}
|
使用checked关键字修饰temp*2的计算结果,并使用try catch在发生溢出时作处理。以上代码将输出:“溢出了,要处理哟”对象
问题是若是一段代码中有不少算术运算都须要作溢出检查,那会有不少checked修饰的表达式,怎么办呢?请看4blog
4. checked关键字能够修饰一个语句块,请看下面代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int
temp =
int
.MaxValue;
try
{
checked
{
int
num = temp / 20;
int
a = temp * 2;
int
c = temp * 1000;
}
}
catch
(OverflowException)
{
Console.WriteLine(
"溢出了,要处理哟"
);
}
|
以上程序输出结果和3同样
5. checked在避免算术溢出方面颇有用,那么unchecked呢,它有用吗?答案是确定的,有时候咱们不须要准确的计算结果,咱们只是须要那么一个数而已,至于溢出不溢出的关系不大,好比说生成一个对象的HashCode,好比说根据一个算法计算出一个相对随机数,这都是不须要准确结果的。以下代码片断
1
2
3
4
5
6
7
8
9
10
11
12
|
class
Person
{
public
string
Name {
get
;
set
; }
public
string
Title {
get
;
set
; }
public
override
int
GetHashCode()
{
return
unchecked
(Name.GetHashCode() + Title.GetHashCode());
}
}
|
unchecked也能够修饰语句块,其用法和checked彻底同样。
6. checked和unchecked是能够嵌套使用的,虽然没啥意义。语句是不是checked以最近嵌套的checked或者unchecked决定
7. 从IL中看checked关键字
C#代码:
static
void
Main(
string
[] args)
{
int
a =
int
.MaxValue;
int
b = a * 2;
int
c =
checked
(a * 2);
int
d =
unchecked
(a + 3);
Console.Read();
}
|
对应IL
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 26 (0x1a)
.maxstack 2
.locals init ([0] int32 a,
[1] int32 b,
[2] int32 c,
[3] int32 d)
IL_0000: nop
IL_0001: ldc.i4 0x7fffffff
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldc.i4.2
IL_0009: mul
IL_000a: stloc.1
IL_000b: ldloc.0
IL_000c: ldc.i4.2
IL_000d: mul.ovf
IL_000e: stloc.2
IL_000f: ldloc.0
IL_0010: ldc.i4.3
IL_0011: add
IL_0012: stloc.3
IL_0013: call int32 [mscorlib]System.Console::Read()
IL_0018: pop
IL_0019: ret
} // end of method Program::Main
请看IL中的红色和绿色加剧显示代码,能够看出使用checked时,IL的运算是mul.ovf;不使用checked或者使用unchecked时的IL运算函数是mul或者add,不带.ovf。
8. checked或者unchecked只影响其包围的语句,不会影响到包围的语句内调用函数的代码块,以下示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
static
void
Main(
string
[] args)
{
int
a =
int
.MaxValue;
int
b = 20;
checked
{
int
c = TestMethod(a, b);
Console.WriteLine(c);
}
}
static
int
TestMethod(
int
a,
int
b)
{
return
a * b;
}
|
上面代码将会正常执行,checked语句块并未起到应有的做用。
9. 全局开启或者关闭checked编译选项
在项目属性页上选择“生成”选项卡,而后点击“高级”按钮,选中“检查数学运算溢出”选项,以下示意图
总结:
checked和unchecked是两个不经常使用的关键字,可是他们俩是有用的,在须要的时候请记得用他们两位,另外建议测试时开启全局checked编译器选项。
转自:https://www.cnblogs.com/yukaizhao/archive/2011/08/09/csharp-checked-unchecked-keywords.html