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# 참조