我发现了一些关于 C# 原始数据类型的有趣之处;
C#的long
整数类型范围是-2^63 ~ 2^63-1;
long.MaxValue = 9223372036854775807
而int
type 的范围是 -2^31 ~ 2^31-1。
int.MaxValue = 2147483647
所以现在问题来了。
这是程序。
static void Main(string[] args)
{
Console.WriteLine($"long.Max: {long.MaxValue}");
Console.WriteLine($"int.Max: {int.MaxValue}");
int f = int.MaxValue;
long x = f + 1;
Console.WriteLine($"int.Max + 1: {x}");
}
Run Code Online (Sandbox Code Playgroud)
我通过命令运行此代码dotnet run
。我得到了这个:
long.Max: 9223372036854775807
int.Max: 2147483647
int.Max + 1: -2147483648
Run Code Online (Sandbox Code Playgroud)
那么,为什么值x
是-2147483648
不是2147483648
?