asp.net中的check與uncheck關鍵字用法解析_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了asp.net中的check與uncheck關鍵字用法。分享給大家供大家參考。具體分析如下:

checked和unchecked是兩個不常用的關鍵字,但是確是非常有用的關鍵字,對此,建議測試時開啟全域checked編譯器選項。

1. 一段編譯沒經由過程的代碼

複製代碼 代碼如下:
int a = int.MaxValue * 2;

以上程式碼片段編譯沒有經由過程,在VS2010中會有一條紅色的波浪線指出這段代碼有題目:”The operation overflows at compile time in checked mode”。這說瞭然編譯器會在編譯時間搜檢數學運算是否溢出。然則編譯時間能搜檢出溢出的景象僅限於應用常量的運算。2中的代碼編譯器就不報不失足誤來了。

2. 一段編譯經由過程然則不克不及獲得正確成果的代碼

複製代碼 代碼如下:
int temp = int.MaxValue;
int a = temp * 2;
Console.Write(a);

我先把常量int.MaxValue的值給了姑且變數temp,然後應用姑且變數乘以2策畫成果賦值給a;這段代碼是可以正常履行的,履行成果將輸出 -2。
這申明在運行時預設景象法度是不會搜檢算術運算是否溢出的,cpu儘管算,對於它來講按規矩算就是了,成果對不合錯誤不是他的錯。
正常履行了,而成果是錯誤的,這是很是危險的景象,該如何避免這種危險呢?請看3

3. 應用checked關鍵字,溢出時警示

複製代碼 代碼如下:
int temp = int.MaxValue;
try
{
    int a = checked(temp * 2);
     Console.WriteLine(a);
}
catch (OverflowException)
{
Console.WriteLine("溢出了,要處理懲罰喲");
}

應用checked關鍵字潤飾temp*2的策畫成果,並應用try catch在產生溢出時做處理懲罰。以上代碼將輸出:“溢出了,要處理懲罰喲”
題目是若是一段代碼中有很多算術運算都須要做溢出搜檢,那會有很多checked潤飾的運算式,怎麼辦呢?請看4

4. checked關鍵字可以潤飾一個語句塊,請看下面代碼

複製代碼 代碼如下:
int temp = int.MaxValue;
try
{
    checked
    {
        int num = temp / 20;
        int a = temp * 2;
        int c = temp * 1000;
    }
}
catch (OverflowException)
{
    Console.WriteLine("溢出了,要處理懲罰喲");
}

 
以上法度輸出成果和3一樣

5. checked在避免算術溢位方面很有效,那麼unchecked呢,它有效嗎?答案是必然的,有時辰我們不須要正確的策畫成果,我們只是須要那麼一個數罷了,至於溢出不溢出的關係不大,比如說產生一個對象的HashCode,比如說按照一個演算法策畫出一個相對隨機數,這都是不須要正確成果的。如下程式碼片段

複製代碼 代碼如下:
class Person
{
   public string Name { get; set; }
 
    public string Title { get; set; }
 
    public override int GetHashCode()
    {
        return unchecked(Name.GetHashCode() + Title.GetHashCode());
    }
}

unchecked也可以潤飾語句塊,其用法和checked完全一樣。

6. checked和unchecked是可以嵌套應用的,固然沒啥意義。語句是否是checked以比來嵌套的checked或者unchecked決意

7. 從IL中看checked關鍵字
C#代碼:

複製代碼 代碼如下:
static void Main(string[] args)
{
    int a = int.MaxValue;
    int b = a * 2;
    int c = checked(a * 2);
    int d = unchecked(a + 3);
 
    Console.Read();
}

對應IL
複製代碼 代碼如下:
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       26 (0 x1a)
  .maxstack  2
  .locals init ([0] int32 a,
           [1] int32 b,
           [2] int32 c,
           [3] int32 d)
  IL_0000:  nop
  IL_0001:  ldc.i4     0 x7fffffff
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldc.i4.2
  IL_0009:  mul
  IL_000a:  stloc.1
  IL_000b:  ldloc.0
  IL_000c:  ldc.i4.2
  IL_000d:  mul.ovf
  IL_000e:  stloc.2
  IL_000f:  ldloc.0
  IL_0010:  ldc.i4.3
  IL_0011:  add
  IL_0012:  stloc.3
  IL_0013:  call       int32 [mscorlib]System.Console::Read()
  IL_0018:  pop
  IL_0019:  ret
} // end of method Program::Main

請看IL中的紅色和綠色加重顯示代碼,可以看出應用checked時,IL的運算是mul.ovf不應用checked或者應用unchecked時的IL運算函數是mul或者add,不帶.ovf。

8. checked或者unchecked隻影響其包抄的語句,不會影響到包抄的語句內調用函數的代碼塊,如下樣本:

複製代碼 代碼如下:
static void Main(string[] args)
{
    int a = int.MaxValue;
    int b = 20;
    checked
    {
        int c = TestMethod(a, b);
        Console.WriteLine(c);
    }
}
static int TestMethod(int a, int b)
{
    return a * b;
}

上方代碼將會正常履行,checked語句塊並未起到應有的感化。

9. 全域開啟或者封閉checked編譯選項
在項目屬性頁面上選擇“產生”選項卡,然後點擊“高等”按鈕,選中“搜檢數學運算溢出”選項,如下示意圖

希望本文所述對大家的asp.net程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.