using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _0221_checked运算符
{class Program{static void Main(string[] args){// C# checked运算符byte x = 255;checked{x++; // 超出了0至255的范围,溢出}Console.WriteLine("x的值是:" + x);Console.ReadLine();//byte数据类型 只能包含0到255的整数,所以x值的加1会导致溢出}}//?一、C# checked运算符//checked运算符用于对整型算术运算和显式转换启用溢出检查。//默认情况下,表达式产生的值如果超出了目标类型的范围,将会产生两种情况://? 常数表达式将导致编译时错误。//? 变量表达式在运行时计算并将引发异常。//?二、提示//如果我们通过编译器选项或者环境配置在全局范围内取消了溢出检查,就可以使用checked关键字来启用该项功能了。
}