checked 和 unchecked關(guān)鍵字用來限定檢查或者不檢查數(shù)學(xué)運(yùn)算溢出的;如果使用了checked發(fā)生數(shù)學(xué)運(yùn)算溢出時(shí)會(huì)拋出OverflowException;如果使用了unchecked則不會(huì)檢查溢出,算錯(cuò)了也不會(huì)報(bào)錯(cuò)。
1. 一段編譯沒通過的代碼
int a = int.MaxValue * 2;
以上代碼段編譯沒有通過,在VS2010中會(huì)有一條紅色的波浪線指出這段代碼有問題:”The operation overflows at compile time in checked mode”。這說明了編譯器會(huì)在編譯時(shí)檢查數(shù)學(xué)運(yùn)算是否溢出。但是編譯時(shí)能檢查出溢出的情況僅限于使用常量的運(yùn)算。2中的代碼編譯器就不報(bào)不出錯(cuò)誤來了。
2. 一段編譯通過但是不能得到正確結(jié)果的代碼
int temp = int.MaxValue;
int a = temp * 2;
Console.Write(a);
我先把常量int.MaxValue的值給了臨時(shí)變量temp,然后使用臨時(shí)變量乘以2計(jì)算結(jié)果賦值給a;這段代碼是可以正常執(zhí)行的,執(zhí)行結(jié)果將輸出 -2。
這說明在運(yùn)行時(shí)默認(rèn)情況程序是不會(huì)檢查算術(shù)運(yùn)算是否溢出的,cpu只管算,對于它來講按規(guī)則算就是了,結(jié)果對不對不是他的錯(cuò)。
正常執(zhí)行了,而結(jié)果是錯(cuò)誤的,這是非常危險(xiǎn)的情況,該如何避免這種危險(xiǎn)呢?請看3
3. 使用checked關(guān)鍵字,溢出時(shí)報(bào)警
int temp = int.MaxValue;
try
{
int a = checked(temp * 2);
Console.WriteLine(a);
}
catch (OverflowException)
{
Console.WriteLine("溢出了,要處理喲");
}
使用checked關(guān)鍵字修飾temp*2的計(jì)算結(jié)果,并使用try catch在發(fā)生溢出時(shí)做處理。以上代碼將輸出:“溢出了,要處理喲”
問題是如果一段代碼中有很多算術(shù)運(yùn)算都需要做溢出檢查,那會(huì)有很多checked修飾的表達(dá)式,怎么辦呢?請看4
4. checked關(guān)鍵字可以修飾一個(gè)語句塊,請看下面代碼
int temp = int.MaxValue;
try
{
checked
{
int num = temp / 20;
int a = temp * 2;
int c = temp * 1000;
}
}
catch (OverflowException)
{
Console.WriteLine("溢出了,要處理喲");
}
以上程序輸出結(jié)果和3一樣
5. checked在避免算術(shù)溢出方面很有用,那么unchecked呢,它有用嗎?答案是肯定的,有時(shí)候我們不需要準(zhǔn)確的計(jì)算結(jié)果,我們只是需要那么一個(gè)數(shù)而已,至于溢出不溢出的關(guān)系不大,比如說生成一個(gè)對象的HashCode,比如說根據(jù)一個(gè)算法計(jì)算出一個(gè)相對隨機(jī)數(shù),這都是不需要準(zhǔn)確結(jié)果的。如下代碼片段
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關(guān)鍵字
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();
}
對應(yīng)IL
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 26 (0x1a)
.maxstack 2
.locals init ([0] int32 a,
[1] int32 b,
[2] int32 c,
[3] int32 d)
IL_0000: nop
IL_0001: ldc.i4 0x7fffffff
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時(shí),IL的運(yùn)算是mul.ovf;不使用checked或者使用unchecked時(shí)的IL運(yùn)算函數(shù)是mul或者add,不帶.ovf。
8. checked或者unchecked只影響其包圍的語句,不會(huì)影響到包圍的語句內(nèi)調(diào)用函數(shù)的代碼塊,如下示例:
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;
}
上面代碼將會(huì)正常執(zhí)行,checked語句塊并未起到應(yīng)有的作用。
9. 全局開啟或者關(guān)閉checked編譯選項(xiàng)
在項(xiàng)目屬性頁上選擇“生成”選項(xiàng)卡,然后點(diǎn)擊“高級(jí)”按鈕,選中“檢查數(shù)學(xué)運(yùn)算溢出”選項(xiàng),如下示意圖

總結(jié):
checked和unchecked是兩個(gè)不常用的關(guān)鍵字,但是他們倆是有用的,在需要的時(shí)候請記得用他們兩位,另外建議測試時(shí)開啟全局checked編譯器選項(xiàng),謝謝。