Aracılığıyla paylaş


checked (C# Reference)

checked Anahtar sözcüğünü açıkça taşma integral tür aritmetik işlemler ve dönüşümler için denetimini etkinleştirmek için kullanılır.

Varsayılan olarak, yalnızca sabit değerleri içeren bir deyim ifade hedef türü aralık dışında bir değer üretir, derleyici hatası neden olur.İfade, bir veya daha fazla sabit olmayan değerler içeriyorsa, derleyici taşma algılamaz.Atanan ifadenin değeri hesaplanırken i2 aşağıdaki örnekte bir derleyici hatası neden olmaz.

// 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.
Console.WriteLine(i2);

Varsayılan olarak, bu sabit olmayan ifadeler için taşma zamanında ya da denetlenmez ve taşma özel durumlar yükseltmeyin.Önceki örnekte,-2,147,483,639 iki pozitif tamsayılar toplamı olarak görüntüler.

Taşma denetimini etkin olabilir derleyici seçenekleri, ortamı yapılandırması veya kullanımını checked anahtar sözcüğü.Aşağıdaki örnekler nasıl kullanılacağını gösteren bir checked ifade veya bir checked blok önceki toplam çalışma zamanında üretilen taşma algılamak için.Örneklerin her ikisinde bir taşma özel durum.

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

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

// Checked block.
checked
{
    int i3 = 2147483647 + ten;
    Console.WriteLine(i3);
}

Denetlenmeyen anahtar sözcüğünü taşma denetimini önlemek için kullanılabilir.

Örnek

Bu örnek, nasıl kullanılacağını gösteren checked taşma zamanında denetimini etkinleştirmek için.

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 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;
        try
        {
            // 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}",
                          CheckedMethod());
        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
 */
}

C# dil belirtiminin

Daha fazla bilgi için bkz: C# dil belirtiminin. Dil belirtimi, C# sözdizimi ve kullanımı için nihai kaynaktır.

Ayrıca bkz.

Başvuru

C# anahtar sözcükler

İşaretli ve denetlenmeyen (C# Reference)

denetlenmeyen (C# Reference)

Kavramlar

C# Programlama Kılavuzu

Diğer Kaynaklar

C# Başvurusu