it编程 > 编程语言 > Asp.net

C# checked和unchecked的使用小结

36人参与 2025-07-31 Asp.net

一、概述

二、语句

具体使用见下面案例:

     	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的时候不会做溢出检查,不会有异常

        

三、表达式

具体使用见下面案例:

    		//【二】检查表达式 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

具体使用见下面案例:

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

注意,在前面的示例中:

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

参考资料:

已选中和未选中的语句(c# 参考)

到此这篇关于c# checked和unchecked的使用小结的文章就介绍到这了,更多相关c# checked unchecked 内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

C# 可空值类型的具体使用

07-31

C# MemoryStream中ToArray和GetBuffer的区别小小结

07-31

C# MemoryStream的具体使用

07-31

C# WinForm实现Socket异步通讯的步骤详解

07-30

C# System.Text.Encoding使用小结

07-30

C# SerialPort类中清空缓存区的两种方法

07-30

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论