unchecked(C# 参考)unchecked (C# Reference)

unchecked 关键字用于取消整型类型的算术运算和转换的溢出检查。The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.

在未经检查的上下文中,如果表达式生成的值超出目标类型的范围,则不会标记溢出。In an unchecked context, if an expression produces a value that is outside the range of the destination type, the overflow is not flagged. 例如,由于以下示例中的计算在 unchecked 块或表达式中执行,因此将忽略计算结果对于整数而言过大的事实,并且向 int1 赋予值 -2,147,483,639。For example, because the calculation in the following example is performed in an unchecked block or expression, the fact that the result is too large for an integer is ignored, and int1 is assigned the value -2,147,483,639.

unchecked
{
    int1 = 2147483647 + 10;
}
int1 = unchecked(ConstantMax + 10);

如果删除 unchecked 环境,会发生编译错误。If the unchecked environment is removed, a compilation error occurs. 由于表达式的所有项都是常量,因此可在编译时检测到溢出。The overflow can be detected at compile time because all the terms of the expression are constants.

在编译时和运行时,默认不检查包含非常数项的表达式。Expressions that contain non-constant terms are unchecked by default at compile time and run time. 请参阅启用检查,获取有关使用启用了检查的环境的信息。See checked for information about enabling a checked environment.

由于检查溢出需要时间,因此在没有溢出风险的情况下使用取消检查的代码可能会提高性能。Because checking for overflow takes time, the use of unchecked code in situations where there is no danger of overflow might improve performance. 但是,如果存在溢出的可能,则应使用启用了检查的环境。However, if overflow is a possibility, a checked environment should be used.

示例Example

此示例演示如何使用 unchecked关键字。This sample shows how to use the unchecked keyword.

class UncheckedDemo
{
    static void Main(string[] args)
    {
        // int.MaxValue is 2,147,483,647.
        const int ConstantMax = int.MaxValue;
        int int1;
        int int2;
        int variableMax = 2147483647;

        // The following statements are checked by default at compile time. They do not
        // compile.
        //int1 = 2147483647 + 10;
        //int1 = ConstantMax + 10;

        // To enable the assignments to int1 to compile and run, place them inside 
        // an unchecked block or expression. The following statements compile and
        // run.
        unchecked
        {
            int1 = 2147483647 + 10;
        }
        int1 = unchecked(ConstantMax + 10);

        // The sum of 2,147,483,647 and 10 is displayed as -2,147,483,639.
        Console.WriteLine(int1);


        // The following statement is unchecked by default at compile time and run 
        // time because the expression contains the variable variableMax. It causes  
        // overflow but the overflow is not detected. The statement compiles and runs.
        int2 = variableMax + 10;

        // Again, the sum of 2,147,483,647 and 10 is displayed as -2,147,483,639.
        Console.WriteLine(int2);

        // To catch the overflow in the assignment to int2 at run time, put the
        // declaration in a checked block or expression. The following
        // statements compile but raise an overflow exception at run time.
        checked
        {
            //int2 = variableMax + 10;
        }
        //int2 = checked(variableMax + 10);

        // Unchecked sections frequently are used to break out of a checked 
        // environment in order to improve performance in a portion of code 
        // that is not expected to raise overflow exceptions.
        checked
        { 
            // Code that might cause overflow should be executed in a checked
            // environment.
            unchecked
            { 
                // This section is appropriate for code that you are confident 
                // will not result in overflow, and for which performance is 
                // a priority.
            }
            // Additional checked code here. 
        }
    }
}

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