checked(C# 参考)checked (C# Reference)

checked 关键字用于对整型类型算术运算和转换显式启用溢出检查。The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.

默认情况下,如果表达式仅包含常量值,且产生的值在目标类型范围之外,则会导致编译器错误。By default, an expression that contains only constant values causes a compiler error if the expression produces a value that is outside the range of the destination type. 如果表达式包含一个或多个非常量值,则编译器不检测溢出。If the expression contains one or more non-constant values, the compiler does not detect the overflow. 在下面的示例中,计算赋给 i2 的表达式不会导致编译器错误。Evaluating the expression assigned to i2 in the following example does not cause a compiler error.

// The following example causes compiler error CS0220 because 2147483647
// is the maximum value for integers. 
//int i1 = 2147483647 + 10;

// 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.

默认情况下,在运行时也不检查这些非常量表达式是否溢出,这些表达式不引发溢出异常。By default, these non-constant expressions are not checked for overflow at run time either, and they do not raise overflow exceptions. 上面的示例显示 -2,147,483,639 作为两个正整数之和。The previous example displays -2,147,483,639 as the sum of two positive integers.

可以通过编译器选项、环境配置或使用 checked 关键字来启用溢出检查。Overflow checking can be enabled by compiler options, environment configuration, or use of the checked keyword. 下面的示例演示如何使用 checked 表达式或 checked 块,在运行时检测由前面的求和计算导致的溢出。The following examples demonstrate how to use a checked expression or a checked block to detect the overflow that is produced by the previous sum at run time. 两个示例都引发溢出异常。Both examples raise an overflow exception.

// If the previous sum is attempted in a checked environment, an 
// OverflowException error is raised.

// Checked expression.
Console.WriteLine(checked(2147483647 + ten));

// Checked block.
    int i3 = 2147483647 + ten;

可以使用 unchecked 关键字阻止溢出检查。The unchecked keyword can be used to prevent overflow checking.


此示例演示如何使用 checked 启用运行时溢出检查。This sample shows how to use checked to enable overflow checking at run time.

class OverFlowTest
    // Set maxIntValue to the maximum value for integers.
    static int maxIntValue = 2147483647;

    // Using a checked expression.
    static int CheckedMethod()
        int z = 0;
            // The following line raises an 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;
            // The following calculation is unchecked and will not 
            // raise an exception.
            z = maxIntValue + 10;
        catch (System.OverflowException e)
            // The following line will not be executed.
            Console.WriteLine("UNCHECKED and CAUGHT:  " + e.ToString());
        // Because of the undetected overflow, the sum of 2147483647 + 10 is 
        // returned as -2147483639.
        return z;

    static void Main()
        Console.WriteLine("\nCHECKED output value is: {0}",
        Console.WriteLine("UNCHECKED output value is: {0}",
   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

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also