也许我是基础知识,但我仍在学校学习这个 C# 东西。我知道,如果我将 1 加到最大值 Integer,即 32 位,结果将为负数。我读到 C# 提供了检查和未检查的关键字来处理溢出。Checked 关键字是一些东西,我发现它很有用,但是 unchecked 关键字呢?我真的找不到 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?
}
}