<分区>
我刚刚听说 int 的大小取决于机器。我一直以为int的大小是32位的。有人可以解释这个难题吗?
标签 c#
<分区>
我刚刚听说 int 的大小取决于机器。我一直以为int的大小是32位的。有人可以解释这个难题吗?
最佳答案
在 C# 中,int
类型始终是 Int32
,并且始终是 32 位。
其他一些语言有不同的规则,int
可以是机器相关的。在某些语言中,int
被定义为具有最小大小,但机器/实现特定大小至少有那么大。例如,在 C++ 中,int 数据类型不一定是 32 位。来自 fundimental data types :
the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size.
然而,.NET 对此进行了标准化,因此类型实际上被指定为 Int32
、Int64
等。在 C# 中,int
是一个System.Int32
的别名,始终为 32 位。 C# 语言规范中的 4.1.5 Integral Types
部分保证了这一点,其中指出:
The int type represents signed 32-bit integers with values between –2147483648 and 2147483647.
关于c# - C# 中的 int 是 32 位的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14900220/