【转载】C# checked和unchecked关键字
--转自微软 checked 关键字 - C# 参考 | Microsoft Docs / unchecked 关键字 - C# 参考 | Microsoft Docs
--本文版权归微软所有
一、checked关键字
checked
关键字用于对整型类型算术运算和转换显式启用溢出检查。
默认情况下,如果表达式仅包含常量值,且产生的值在目标类型范围之外,则会导致编译器错误。 如果表达式包含一个或多个非常量值,则编译器不检测溢出。 在下面的示例中,计算赋给 i2
的表达式不会导致编译器错误。
1 // The following example causes compiler error CS0220 because 2147483647 2 // is the maximum value for integers. 3 //int i1 = 2147483647 + 10; 4 5 // The following example, which includes variable ten, does not cause 6 // a compiler error. 7 int ten = 10; 8 int i2 = 2147483647 + ten; 9 10 // By default, the overflow in the previous statement also does 11 // not cause a run-time exception. The following line displays 12 // -2,147,483,639 as the sum of 2,147,483,647 and 10. 13 Console.WriteLine(i2);
默认情况下,在运行时也不检查这些非常量表达式是否溢出,这些表达式不引发溢出异常。 上面的示例显示 -2,147,483,639 作为两个正整数之和。
可以通过编译器选项、环境配置或使用 checked
关键字来启用溢出检查。 下面的示例演示如何使用 checked
表达式或 checked
块,在运行时检测由前面的求和计算导致的溢出。 两个示例都引发溢出异常。
1 // If the previous sum is attempted in a checked environment, an 2 // OverflowException error is raised. 3 4 // Checked expression. 5 Console.WriteLine(checked(2147483647 + ten)); 6 7 // Checked block. 8 checked 9 { 10 int i3 = 2147483647 + ten; 11 Console.WriteLine(i3); 12 }
可以使用 unchecked 关键字阻止溢出检查。
示例
此示例演示如何使用 checked
启用运行时溢出检查。
1 class OverFlowTest 2 { 3 // Set maxIntValue to the maximum value for integers. 4 static int maxIntValue = 2147483647; 5 6 // Using a checked expression. 7 static int CheckedMethod() 8 { 9 int z = 0; 10 try 11 { 12 // The following line raises an exception because it is checked. 13 z = checked(maxIntValue + 10); 14 } 15 catch (System.OverflowException e) 16 { 17 // The following line displays information about the error. 18 Console.WriteLine("CHECKED and CAUGHT: " + e.ToString()); 19 } 20 // The value of z is still 0. 21 return z; 22 } 23 24 // Using an unchecked expression. 25 static int UncheckedMethod() 26 { 27 int z = 0; 28 try 29 { 30 // The following calculation is unchecked and will not 31 // raise an exception. 32 z = maxIntValue + 10; 33 } 34 catch (System.OverflowException e) 35 { 36 // The following line will not be executed. 37 Console.WriteLine("UNCHECKED and CAUGHT: " + e.ToString()); 38 } 39 // Because of the undetected overflow, the sum of 2147483647 + 10 is 40 // returned as -2147483639. 41 return z; 42 } 43 44 static void Main() 45 { 46 Console.WriteLine("\nCHECKED output value is: {0}", 47 CheckedMethod()); 48 Console.WriteLine("UNCHECKED output value is: {0}", 49 UncheckedMethod()); 50 } 51 /* 52 Output: 53 CHECKED and CAUGHT: System.OverflowException: Arithmetic operation resulted 54 in an overflow. 55 at ConsoleApplication1.OverFlowTest.CheckedMethod() 56 57 CHECKED output value is: 0 58 UNCHECKED output value is: -2147483639 59 */ 60 }
二、unchecked关键字
unchecked
关键字用于取消整型类型的算术运算和转换的溢出检查。
在未经检查的上下文中,如果表达式生成的值超出目标类型的范围,则不会标记溢出。 例如,由于以下示例中的计算在 unchecked
块或表达式中执行,因此将忽略计算结果对于整数而言过大的事实,并且向 int1
赋予值 -2,147,483,639。
1 unchecked 2 { 3 int1 = 2147483647 + 10; 4 } 5 int1 = unchecked(ConstantMax + 10);
如果删除 unchecked
环境,会发生编译错误。 由于表达式的所有项都是常量,因此可在编译时检测到溢出。
在编译时和运行时,默认不检查包含非常数项的表达式。 请参阅启用检查,获取有关使用启用了检查的环境的信息。
由于检查溢出需要时间,因此在没有溢出风险的情况下使用取消检查的代码可能会提高性能。 但是,如果存在溢出的可能,则应使用启用了检查的环境。
示例
此示例演示如何使用 unchecked
关键字。
1 class UncheckedDemo 2 { 3 static void Main(string[] args) 4 { 5 // int.MaxValue is 2,147,483,647. 6 const int ConstantMax = int.MaxValue; 7 int int1; 8 int int2; 9 int variableMax = 2147483647; 10 11 // The following statements are checked by default at compile time. They do not 12 // compile. 13 //int1 = 2147483647 + 10; 14 //int1 = ConstantMax + 10; 15 16 // To enable the assignments to int1 to compile and run, place them inside 17 // an unchecked block or expression. The following statements compile and 18 // run. 19 unchecked 20 { 21 int1 = 2147483647 + 10; 22 } 23 int1 = unchecked(ConstantMax + 10); 24 25 // The sum of 2,147,483,647 and 10 is displayed as -2,147,483,639. 26 Console.WriteLine(int1); 27 28 // The following statement is unchecked by default at compile time and run 29 // time because the expression contains the variable variableMax. It causes 30 // overflow but the overflow is not detected. The statement compiles and runs. 31 int2 = variableMax + 10; 32 33 // Again, the sum of 2,147,483,647 and 10 is displayed as -2,147,483,639. 34 Console.WriteLine(int2); 35 36 // To catch the overflow in the assignment to int2 at run time, put the 37 // declaration in a checked block or expression. The following 38 // statements compile but raise an overflow exception at run time. 39 checked 40 { 41 //int2 = variableMax + 10; 42 } 43 //int2 = checked(variableMax + 10); 44 45 // Unchecked sections frequently are used to break out of a checked 46 // environment in order to improve performance in a portion of code 47 // that is not expected to raise overflow exceptions. 48 checked 49 { 50 // Code that might cause overflow should be executed in a checked 51 // environment. 52 unchecked 53 { 54 // This section is appropriate for code that you are confident 55 // will not result in overflow, and for which performance is 56 // a priority. 57 } 58 // Additional checked code here. 59 } 60 } 61 }
本文转自微软Docs,如有错误欢迎指正。