<21天学通C#>使用unchecked使得命令编辑器不对代码进行检查,使用checked对代码进行检查

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 使用unchecked使得命令编辑器不对代码进行检查
{
class Program
{
static void Main(string[] args)
{
int val1 = 2147483647;
int val2 ;
unchecked
{
val2 = val1 + 1;
}
Console.WriteLine("val1 is {0}", val1);
Console.WriteLine("val2 is {0}", val2);
Console.WriteLine("按任意键退出");
Console.ReadKey();
}
}
}

改为checked:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 使用unchecked使得命令编辑器不对代码进行检查
{
class Program
{
static void Main(string[] args)
{
int val1 = 2147483647;
int val2 ;
checked
{
val2 = val1 + 1;
}
Console.WriteLine("val1 is {0}", val1);
Console.WriteLine("val2 is {0}", val2);
Console.WriteLine("按任意键退出");
Console.ReadKey();
}
}
}

 

 

posted on 2018-11-08 14:51  天源  阅读(182)  评论(0编辑  收藏  举报

导航