也许我是基础知识,但我还在学校学习这个C#的东西.据我所知,如果我将1加到最大值整数,哪一个是32位,结果将是负数.我读到C#提供了检查和未检查的关键字来处理溢出.已检查的关键字是一种东西,我发现它很有用,但未经检查的关键字怎么样?我真的找不到用于unchecked -keyworded块的有用的东西.有没有?接下来的两种方法如何相互不同?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Practice_6
{
class Program
{
static void Main(string[] args)
{
int value = Int32.MaxValue;
value++;
//Approach 1 to make a decision
if (value > Int32.MaxValue) {
//Do something
}
value = Int32.MaxValue;
//Approach 2 to make a decision
unchecked {
value++;
//Do something
}
//What's the difference between these two approaches to handle overflow?
}
}
Run Code Online (Sandbox Code Playgroud)