找回密码
 立即注册
首页 业界区 业界 c# checked unchecked 关键字,在需要的时候请记得用他 ...

c# checked unchecked 关键字,在需要的时候请记得用他们两位

师悠逸 2025-5-29 15:53:58
checked 和 unchecked关键字用来限定检查或者不检查数学运算溢出的;如果使用了checked发生数学运算溢出时会抛出OverflowException;如果使用了unchecked则不会检查溢出,算错了也不会报错。
1. 一段编译没通过的代码
  1. int a = int.MaxValue * 2;
复制代码
以上代码段编译没有通过,在VS2010中会有一条红色的波浪线指出这段代码有问题:”The operation overflows at compile time in checked mode”。这说明了编译器会在编译时检查数学运算是否溢出。但是编译时能检查出溢出的情况仅限于使用常量的运算。2中的代码编译器就不报不出错误来了。

2. 一段编译通过但是不能得到正确结果的代码
  1. int temp = int.MaxValue;
  2. int a = temp * 2;
  3. Console.Write(a);
复制代码
我先把常量int.MaxValue的值给了临时变量temp,然后使用临时变量乘以2计算结果赋值给a;这段代码是可以正常执行的,执行结果将输出 -2。
这说明在运行时默认情况程序是不会检查算术运算是否溢出的,cpu只管算,对于它来讲按规则算就是了,结果对不对不是他的错。
正常执行了,而结果是错误的,这是非常危险的情况,该如何避免这种危险呢?请看3

3. 使用checked关键字,溢出时报警
  1. int temp = int.MaxValue;
  2. try
  3. {
  4.     int a = checked(temp * 2);
  5.     Console.WriteLine(a);
  6. }
  7. catch (OverflowException)
  8. {
  9. Console.WriteLine("溢出了,要处理哟");
  10. }
复制代码
使用checked关键字修饰temp*2的计算结果,并使用try catch在发生溢出时做处理。以上代码将输出:“溢出了,要处理哟”
问题是如果一段代码中有很多算术运算都需要做溢出检查,那会有很多checked修饰的表达式,怎么办呢?请看4

4. checked关键字可以修饰一个语句块,请看下面代码
  1. int temp = int.MaxValue;
  2. try
  3. {
  4.     checked
  5.     {
  6.         int num = temp / 20;
  7.         int a = temp * 2;
  8.         int c = temp * 1000;
  9.     }
  10. }
  11. catch (OverflowException)
  12. {
  13.     Console.WriteLine("溢出了,要处理哟");
  14. }
复制代码
以上程序输出结果和3一样
5. checked在避免算术溢出方面很有用,那么unchecked呢,它有用吗?答案是肯定的,有时候我们不需要准确的计算结果,我们只是需要那么一个数而已,至于溢出不溢出的关系不大,比如说生成一个对象的HashCode,比如说根据一个算法计算出一个相对随机数,这都是不需要准确结果的。如下代码片段
  1. class Person
  2. {
  3.    public string Name { get; set; }
  4.     public string Title { get; set; }
  5.     public override int GetHashCode()
  6.     {
  7.         return unchecked(Name.GetHashCode() + Title.GetHashCode());
  8.     }
  9. }
复制代码
unchecked也可以修饰语句块,其用法和checked完全一样。
6. checked和unchecked是可以嵌套使用的,虽然没啥意义。语句是否是checked以最近嵌套的checked或者unchecked决定

7. 从IL中看checked关键字
C#代码:
  1. static void Main(string[] args)
  2. {
  3.     int a = int.MaxValue;
  4.     int b = a * 2;
  5.     int c = checked(a * 2);
  6.     int d = unchecked(a + 3);
  7.     Console.Read();
  8. }
复制代码
对应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. static void Main(string[] args)
  2. {
  3.     int a = int.MaxValue;
  4.     int b = 20;
  5.     checked
  6.     {
  7.         int c = TestMethod(a, b);
  8.         Console.WriteLine(c);
  9.     }
  10. }
  11. static int TestMethod(int a, int b)
  12. {
  13.     return a * b;
  14. }
复制代码
上面代码将会正常执行,checked语句块并未起到应有的作用。

9. 全局开启或者关闭checked编译选项
在项目属性页上选择“生成”选项卡,然后点击“高级”按钮,选中“检查数学运算溢出”选项,如下示意图
1.png

总结:
checked和unchecked是两个不常用的关键字,但是他们俩是有用的,在需要的时候请记得用他们两位,另外建议测试时开启全局checked编译器选项,谢谢。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册