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

C# checked和unchecked的使用小結

 更新時間:2025年07月31日 10:38:55   作者:鯉籽鯤  
C#中checked和unchecked控制整型運算溢出檢查,檢查上下文引發(fā)異?;蚓幾g錯誤,下面就來具體介紹一下checked和unchecked的使用,感興趣的可以了解一下

一、概述

  • checked 和 unchecked 語句指定整型類型算術運算和轉換的溢出檢查上下文。

  • 當發(fā)生整數算術溢出時,溢出檢查上下文將定義發(fā)生的情況。

    • 在已檢查的上下文中,引發(fā) System.OverflowException;
    • 如果在常數表達式中發(fā)生溢出,則會發(fā)生編譯時錯誤。
    • 在未檢查的上下文中,會通過丟棄任何不適應目標類型的高序位來將操作結果截斷。

二、語句

  • checkedunchecked 語句指定整型類型算術運算和轉換的溢出檢查上下文。

具體使用見下面案例:

     	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();
        }

四、本地函數與 Checked

  • checked 和 unchecked 語句和運算符僅影響以文本形式存在于語句塊或運算符括號內的操作的溢出檢查上下文

具體使用見下面案例:

      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;
        }
    }

注意,在前面的示例中:

  • 有一個本地函數Multiply ,一個正常的私有函數Multiply2
  • 當使用 Console.WriteLine(Multiply2(factor, int.MaxValue)); 這樣的方式調用 本地函數Multiply 或私有函數Multiply2,checked 語句不會引發(fā)任何異常。
  • 當使用 Console.WriteLine(Multiply2(factor, factor * int.MaxValue)); 這樣的方式調用 本地函數Multiply 或私有函數Multiply2,由于第二個參數 的表達式 會引起 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# 參考)

到此這篇關于C# checked和unchecked的使用小結的文章就介紹到這了,更多相關C# checked unchecked 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論