欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#關(guān)鍵字Check簡單介紹

 更新時(shí)間:2022年04月08日 18:13:25   作者:yue008  
這篇文章主要介紹了C#關(guān)鍵字Check功能描述及注意事項(xiàng),checke關(guān)鍵字主要用于對整型類型算術(shù)運(yùn)算和轉(zhuǎn)換顯式啟用溢出檢查,本文通過程序演示給大家詳細(xì)介紹,需要的朋友一起看看吧

功能描述

checked運(yùn)算符通知運(yùn)行時(shí)當(dāng)溢出時(shí)拋出一個(gè)OverflowException異常,checked運(yùn)算符可以用于++, --, -(一元), +, -, *, /以及整數(shù)類型之間的顯示轉(zhuǎn)換。

用于對整型類型算術(shù)運(yùn)算和轉(zhuǎn)換顯式啟用溢出檢查。

注意事項(xiàng)

1.如果不選擇使用Check關(guān)鍵字,則會(huì)出現(xiàn)數(shù)值溢出,
2.如果使用Check關(guān)鍵字,當(dāng)出現(xiàn)數(shù)值溢出時(shí),會(huì)彈出報(bào)錯(cuò)信息。

程序演示

1.未使用Check關(guān)鍵字,會(huì)出現(xiàn)數(shù)值溢出

  static void Main(string[] args)
        {
            int i = 10;

            Console.WriteLine(2147483647 + i);
            Console.ReadKey();

在這里插入圖片描述

2.使用Check關(guān)鍵字,數(shù)值溢出時(shí),會(huì)觸發(fā)報(bào)錯(cuò)信息

 int i = 10;
            Console.WriteLine(checked(2147483647 + i));
            Console.ReadKey();

在這里插入圖片描述

3.使用 checked 啟用運(yùn)行時(shí)溢出檢查。用到的方法有Try…Catch。

  static int maxIntValue = 2147483647;
       static int CheckMethod()
        {
            int z = 0;
            try
            {
                z = checked(maxIntValue + 10);
            }
            catch (System.OverflowException e)
            {
                Console.WriteLine("Checked and Caught" + e.ToString());

            }
            return z;
        }

        static int UncheckedMethod()
        {
            int z = 0;
            try
            {
                z = maxIntValue + 10;
            }
            catch(System.OverflowException e)
            {
                Console.WriteLine("Unchecked and Caught" + e.ToString());

            }
            return z;

           
        }
        static void Main()
        {
           Console.WriteLine("\nChecked output valuse is:{0}",CheckMethod());
            Console.WriteLine("\nUnChecked output valuse is:{0}",
                UncheckedMethod());
            Console.ReadKey();
        }

在這里插入圖片描述

到此這篇關(guān)于C#Check關(guān)鍵字的介紹的文章就介紹到這了,更多相關(guān)C# Check關(guān)鍵字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論