C# checked和unchecked的使用小結(jié)
更新時間:2025年07月31日 10:38:55 作者:鯉籽鯤
C#中checked和unchecked控制整型運算溢出檢查,檢查上下文引發(fā)異?;蚓幾g錯誤,下面就來具體介紹一下checked和unchecked的使用,感興趣的可以了解一下
一、概述
checked 和 unchecked 語句指定整型類型算術運算和轉(zhuǎn)換的溢出檢查上下文。
當發(fā)生整數(shù)算術溢出時,溢出檢查上下文將定義發(fā)生的情況。
- 在已檢查的上下文中,引發(fā) System.OverflowException;
- 如果在常數(shù)表達式中發(fā)生溢出,則會發(fā)生編譯時錯誤。
- 在未檢查的上下文中,會通過丟棄任何不適應目標類型的高序位來將操作結(jié)果截斷。
二、語句
checked和unchecked語句指定整型類型算術運算和轉(zhuǎn)換的溢出檢查上下文。
具體使用見下面案例:
static void Main(string[] args)
{
uint a = uint.MaxValue;
//【一】檢查代碼段 unchecked{ }
unchecked
{
Console.WriteLine(a + 1); // output: 0
}
try
{
checked
{
Console.WriteLine(a + 1);
}
}
catch (OverflowException e)
{
Console.WriteLine(e.Message); // output: 算術運算導致溢出。
}
//由上可知,當我們使用checked的時候會做溢出檢查,會拋出異常
// 當我們使用unchecked的時候不會做溢出檢查,不會有異常
三、表達式
- 若要為表達式指定溢出檢查上下文,還可以使用 checked 和 unchecked 運算符
具體使用見下面案例:
//【二】檢查表達式 unchecked()
double d = double.MaxValue;
int i = unchecked((int)d);
Console.WriteLine(i); // output: -2147483648
try
{
i = checked((int)d);
}
catch (OverflowException e)
{
Console.WriteLine(e.Message); // output: 算術運算導致溢出。
}
Console.ReadLine();
}
四、本地函數(shù)與 Checked
- checked 和 unchecked 語句和運算符僅影響以文本形式存在于語句塊或運算符括號內(nèi)的操作的溢出檢查上下文
具體使用見下面案例:
static void Main(string[] args)
{
int Multiply(int a, int b) => a * b;
int factor = 2;
try
{
checked
{
Console.WriteLine(Multiply(factor, int.MaxValue)); // output: -2
}
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
try
{
checked
{
Console.WriteLine(Multiply(factor, factor * int.MaxValue));
}
}
catch (OverflowException e)
{
Console.WriteLine(e.Message); // output: Arithmetic operation resulted in an overflow.
}
}
internal class Program
{
static void Main(string[] args)
{
int factor = 2;
try
{
checked
{
Console.WriteLine(Multiply2(factor, int.MaxValue)); // output: -2
}
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
try
{
checked
{
Console.WriteLine(Multiply2(factor, factor * int.MaxValue));
}
}
catch (OverflowException e)
{
Console.WriteLine(e.Message); // output: Arithmetic operation resulted in an overflow.
}
}
private static int Multiply2(int a, int b)
{
return a * b;
}
}
注意,在前面的示例中:
- 有一個本地函數(shù)
Multiply,一個正常的私有函數(shù)Multiply2 - 當使用
Console.WriteLine(Multiply2(factor, int.MaxValue));這樣的方式調(diào)用 本地函數(shù)Multiply或私有函數(shù)Multiply2,checked 語句不會引發(fā)任何異常。 - 當使用
Console.WriteLine(Multiply2(factor, factor * int.MaxValue));這樣的方式調(diào)用 本地函數(shù)Multiply或私有函數(shù)Multiply2,由于第二個參數(shù) 的表達式 會引起 checked 語句的上下文檢查,因此會引發(fā)異常。
static void Main(string[] args)
{
int Multiply(int a, int b) => a * b;
int factor = 2;
try
{
unchecked
{
Console.WriteLine(Multiply(factor, int.MaxValue)); // output: -2
}
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
try
{
unchecked
{
Console.WriteLine(Multiply(factor, factor * int.MaxValue)); // output: -4
}
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
}
- unchecked 則都不會引發(fā)異常。
參考資料:
到此這篇關于C# checked和unchecked的使用小結(jié)的文章就介紹到這了,更多相關C# checked unchecked 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#調(diào)用OutLokk實現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細介紹了如何利用C#調(diào)用OutLokk實現(xiàn)發(fā)送郵件的功能,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#使用itextsharp打印pdf的實現(xiàn)代碼
提到打印,恐怕對于很多人都不會陌生,無論是開發(fā)者,還是非計算機專業(yè)的人員都會接觸到打印,?在.NET中實現(xiàn)PDF打印的組件比較多,例如PDFsharp、Report.NET、sharpPDF、itextSharp等等,今天主要簡單的介紹itextSharp組件,需要的朋友可以參考下2024-04-04

