对于类型转换是否溢出可以用 checked和unchecked运算符

在unchecked上下文中会忽略溢出

在checked 会抛出异常

  ushort sh = 2000;
            byte sb;
            sb = unchecked((byte)sh);
            Console.WriteLine(sb);
            sb = checked((byte)sh);//转换抛出异常
            Console.WriteLine(sb);

c# 溢出上下文检测

也可以用代码块

  unchecked
            {
                checked
                {
                   
                }
            }

 

相关文章:

  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
  • 2021-07-23
  • 2021-11-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
相关资源
相似解决方案