checked 和 unchecked關鍵字用來限定檢查或者不檢查數學運算溢出的;若是使用了checked發生數學運算溢出時會拋出OverflowException;若是使用了unchecked則不會檢查溢出,算錯了也不會報錯。html
1. 一段編譯沒經過的代碼算法
1
|
int
a =
int
.MaxValue * 2;
|
以上代碼段編譯沒有經過,在VS2010中會有一條紅色的波浪線指出這段代碼有問題:」The operation overflows at compile time in checked mode」。這說明了編譯器會在編譯時檢查數學運算是否溢出。可是編譯時能檢查出溢出的狀況僅限於使用常量的運算。2中的代碼編譯器就不報不出錯誤來了。ide
2. 一段編譯經過可是不能獲得正確結果的代碼函數
1
2
3
|
int
temp =
int
.MaxValue;
int
a = temp * 2;
Console.Write(a);
|
我先把常量int.MaxValue的值給了臨時變量temp,而後使用臨時變量乘以2計算結果賦值給a;這段代碼是能夠正常執行的,執行結果將輸出 -2。測試
這說明在運行時默認狀況程序是不會檢查算術運算是否溢出的,cpu只管算,對於它來說按規則算就是了,結果對不對不是他的錯。spa
正常執行了,而結果是錯誤的,這是很是危險的狀況,該如何避免這種危險呢?請看3code
3. 使用checked關鍵字,溢出時報警htm
1
2
3
4
5
6
7
8
9
10
|
int
temp =
int
.MaxValue;
try
{
int
a =
checked
(temp * 2);
Console.WriteLine(a);
}
catch
(OverflowException)
{
Console.WriteLine(
"溢出了,要處理喲"
);
}
|
使用checked關鍵字修飾temp*2的計算結果,並使用try catch在發生溢出時作處理。以上代碼將輸出:「溢出了,要處理喲」對象
問題是若是一段代碼中有不少算術運算都須要作溢出檢查,那會有不少checked修飾的表達式,怎麼辦呢?請看4blog
4. checked關鍵字能夠修飾一個語句塊,請看下面代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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,好比說根據一個算法計算出一個相對隨機數,這都是不須要準確結果的。以下代碼片斷
1
2
3
4
5
6
7
8
9
10
11
12
|
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 (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時,IL的運算是mul.ovf;不使用checked或者使用unchecked時的IL運算函數是mul或者add,不帶.ovf。
8. checked或者unchecked隻影響其包圍的語句,不會影響到包圍的語句內調用函數的代碼塊,以下示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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編譯選項
在項目屬性頁上選擇「生成」選項卡,而後點擊「高級」按鈕,選中「檢查數學運算溢出」選項,以下示意圖
總結:
checked和unchecked是兩個不經常使用的關鍵字,可是他們倆是有用的,在須要的時候請記得用他們兩位,另外建議測試時開啓全局checked編譯器選項。
轉自:https://www.cnblogs.com/yukaizhao/archive/2011/08/09/csharp-checked-unchecked-keywords.html