unchecked (C# リファレンス)

unchecked キーワードは、整数型の算術演算および変換に対してオーバーフロー チェックを抑制するのに使用します。

unchecked コンテキストでは、式がチェック先の型の範囲外にある値を生成した場合、オーバーフローにはフラグが付きません。 たとえば、次の例では unchecked ブロックまたは式で計算が実行されるため、結果が整数に対して大きすぎることは無視され、int1 に値 -2,147,483,639 が代入されます。

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

unchecked 環境を削除すると、コンパイル エラーが発生します。 式のすべての項が定数であるため、オーバーフローはコンパイル時に検出できます。

非定数項を含む式は、既定では、コンパイル時にも実行時にもチェックされません。 checked 環境の有効化については、「checked (C# リファレンス)」を参照してください。

オーバーフローのチェックには時間がかかるため、オーバーフローの危険がない状況で unchecked コードを使用するとパフォーマンスが向上する場合があります。 ただし、オーバーフローの可能性がある場合は checked 環境を使用してください。

使用例

unchecked キーワードの使用方法を次の例に示します。

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# 言語仕様」を参照してください。 言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。

参照

参照

C# のキーワード

Checked と Unchecked (C# リファレンス)

checked (C# リファレンス)

概念

C# プログラミング ガイド

その他の技術情報

C# リファレンス