The checked keyword is used to explicitly enable overflow checking on integer arithmetic operations and transformations.
By default, if an expression contains only a constant value and the resulting value is outside the range of the target type, it causes a compiler error. If the expression contains one or more very numeric values, the compiler does not detect overflow. In the following example, the calculation of an expression assigned to I2 does not cause a compiler error.
The following example causes compiler error CS0220 because 2147483647//is the maximum value for integers. int i1 = 2147483647 + ten; The following example, which includes variable ten, does not cause//a compiler error. int ten = 10; int i2 = 2147483647 + ten; By default, the overflow in the previous statement also does//not cause a Run-time exception. The following line displays // -2,147,483,639 as the sum of 2,147,483,647 and 10. Console.WriteLine (I2);
By default, these non-number expressions are not checked for overflow at run time, and these expressions do not throw overflow exceptions. The above example shows 2,147,483,639 as the sum of two positive integers.
Overflow checking can be enabled through compiler options, environment configuration, or by using the checked keyword. The following example shows how to use a checked expression or a checked block to detect an overflow caused by a preceding summation calculation at run time. Two examples all throw an overflow exception.
If the previous sum is attempted in a checked environment, AN//OverflowException error is raised.//checked Expressio N.console.writeline (checked (2147483647 + ten));//checked block.checked{ int i3 = 2147483647 + ten; Console.WriteLine (i3);}
You can use unchecked to cancel overflow checking
This example shows how to enable run-time overflow checking using checked.
Class overflowtest{//Set maxintvalue to the maximum value for integers. static int maxintvalue = 2147483647; Using a checked expression. static int Checkedmethod () {int z = 0; try {//The following line raises a exception because it is checked. z = checked (maxintvalue + 10); } catch (System.OverflowException e) {//The following line displays information about the error . Console.WriteLine ("CHECKED and Caught:" + e.tostring ()); }//The value of Z is still 0. return z; }//Using an unchecked expression. static int Uncheckedmethod () {int z = 0; try {//The following calculation is unchecked and won't//raise an exception. z = maxintvalue + 10; } catch (System.OverflowException e) {//The following line is not being executed. Console.WriteLine("Unchecked and Caught:" + e.tostring ()); }//Because of the undetected overflow, the sum of 2147483647 + Ten is//returned as-2147483639. return z; } static void Main () {Console.WriteLine ("\nchecked output value is: {0}", Checkedme Thod ()); Console.WriteLine ("Unchecked output value is: {0}", Uncheckedmethod ()); }/* output:checked and CAUGHT:System.OverflowException:Arithmetic operation resulted in an overflow. At ConsoleApplication1.OverFlowTest.CheckedMethod () CHECKED output value is:0 Unchecked output value is: 2147483639 */}
The above is the C # checked and unchecked usage content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!