C# checked與unchecked用法

黄舟
發布: 2016-12-28 10:47:53
原創
1773 人瀏覽過

checked 關鍵字用於對整數算術運算和轉換明確啟用溢位檢查。

預設情況下,如果表達式僅包含常數值,且產生的值在目標類型範圍之外,則它會導致編譯器錯誤。 如果表達式包含一個或多個非常數值,則編譯器不偵測溢位。 在下面的範例中,計算賦給 i2 的表達式不會導致編譯器錯誤。

// 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);
登入後複製

預設情況下,在運行時也不檢查這些非常數表達式是否溢出,這些表達式不會引發溢出異常。上面的範例顯示 -2,147,483,639 作為兩個正整數總和。

可以透過編譯器選項、環境配置或使用 checked 關鍵字來啟用溢位檢查。 下面的範例示範如何使用 checked 表達式或 checked 區塊,並在執行時偵測由前面的求和計算所導致的溢位。 兩個範例都引發溢位異常。

// 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);
}
登入後複製

可以使用unchecked取消溢位檢查

此範例示範如何使用 checked 啟用執行時間溢位檢查。

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# checked與unchecked用法的內容,更多相關內容請關注PHP中文網(www.php.cn)!


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!