简体   繁体   English

int是64位C#中的64位整数吗?

[英]Is an int a 64-bit integer in 64-bit C#?

In my C# source code I may have declared integers as: 在我的C#源代码中,我可能将整数声明为:

int i = 5;

or 要么

Int32 i = 5;

In the currently prevalent 32-bit world they are equivalent. 在当前流行的32位环境中,它们是等效的。 However, as we move into a 64-bit world, am I correct in saying that the following will become the same? 但是,当我们进入64位世界时,我是否正确地说以下内容将是相同的?

int i = 5;
Int64 i = 5;

No. The C# specification rigidly defines that int is an alias for System.Int32 with exactly 32 bits. 否。C#规范严格地将int定义为System.Int32的别名,其位数恰好为32位。 Changing this would be a major breaking change. 更改此设置将是重大的重大更改。

The int keyword in C# is defined as an alias for the System.Int32 type and this is (judging by the name) meant to be a 32-bit integer. C#中的int关键字被定义为System.Int32类型的别名,并且(根据名称判断)它是32位整数。 To the specification: 规格:

CLI specification section 8.2.2 (Built-in value and reference types) has a table with the following: CLI规范的第8.2.2节(内置值和引用类型)具有一个包含以下内容的表:

  • System.Int32 - Signed 32-bit integer System.Int32符号的32位整数

C# specification section 8.2.1 (Predefined types) has a similar table: C#规范第8.2.1节(预定义类型)具有类似的表:

  • int - 32-bit signed integral type int -32位有符号整数类型

This guarantees that both System.Int32 in CLR and int in C# will always be 32-bit. 这样可以保证CLR中的System.Int32和C#中的int始终为32位。

Will sizeof(testInt) ever be 8? sizeof(testInt)会是8吗?

No, sizeof(testInt) is an error. 否,sizeof(testInt)是错误。 testInt is a local variable. testInt是局部变量。 The sizeof operator requires a type as its argument. sizeof运算符需要一个类型作为其参数。 This will never be 8 because it will always be an error. 永远不会是8,因为它将始终是错误。

VS2010 compiles ac# managed integer as 4 bytes, even on a 64 bit machine. VS2010甚至在64位计算机上也将ac#托管整数编译为4个字节。

Correct. 正确。 I note that section 18.5.8 of the C# specification defines sizeof(int) as being the compile-time constant 4. That is, when you say sizeof(int) the compiler simply replaces that with 4; 我注意到C#规范的18.5.8节将sizeof(int)定义为编译时常量4。也就是说,当您说sizeof(int) ,编译器将其简单地替换为4;否则,编译器将其替换为4。 it is just as if you'd said "4" in the source code. 就像您在源代码中说“ 4”一样。

Does anyone know if/when the time will come that a standard "int" in C# will be 64 bits? 有谁知道何时/何时将C#中的标准“ int”设为64位?

Never. 决不。 Section 4.1.4 of the C# specification states that "int" is a synonym for "System.Int32". C#规范的第4.1.4节指出“ int”是“ System.Int32”的同义词。

If what you want is a "pointer-sized integer" then use IntPtr. 如果您要的是“指针大小的整数”,请使用IntPtr。 An IntPtr changes its size on different architectures. IntPtr在不同的体系结构上更改其大小。

int is always synonymous with Int32 on all platforms. 在所有平台上, int始终都是Int32同义词。

It's very unlikely that Microsoft will change that in the future, as it would break lots of existing code that assumes int is 32-bits. Microsoft极不可能在将来更改它,因为它将破坏许多现有的假定int是32位的代码。

我认为您可能会感到困惑的是, intInt32的别名,因此它将始终为4个字节,但是IntPtr假定与CPU体系结构的字长匹配,因此在32位系统上它将为4个字节,在64位系统上为8个字节。

According to the C# specification ECMA-334 , section "11.1.4 Simple Types", the reserved word int will be aliased to System.Int32 . 根据C#规范ECMA-334的 “ 11.1.4简单类型”部分,保留字int将别名为System.Int32 Since this is in the specification it is very unlikely to change. 由于这是规范中的内容,因此更改的可能性很小。

无论您使用CLR的32位版本还是64位版本,在C#中int始终表示System.Int32long始终表示System.Int64

The following will always be true in C#: 在C#中,以下始终是正确的

sbyte signed 8 bits, 1 byte sbyte带符号的8位,1字节

byte unsigned 8 bits, 1 byte 字节无符号8位,1字节

short signed 16 bits, 2 bytes 符号16位,2字节

ushort unsigned 16 bits, 2 bytes ushort无符号16位,2字节

int signed 32 bits, 4 bytes int有符号32位,4字节

uint unsigned 32 bits, 4 bytes uint无符号32位,4字节

long signed 64 bits, 8 bytes 长带符号的64位8字节

ulong unsigned 64 bits, 8 bytes ulong无符号64位,8字节

An integer literal is just a sequence of digits (eg 314159 ) without any of these explicit types. 整数文字只是数字序列(例如314159 ), 没有任何这些显式类型。 C# assigns it the first type in the sequence ( int , uint , long , ulong ) in which it fits. C#为其分配适合的序列中的第一个类型( intuintlongulong )。 This seems to have been slightly muddled in at least one of the responses above. 至少在上述回复之一中,似乎有些混淆。

Weirdly the unary minus operator (minus sign) showing up before a string of digits does not reduce the choice to ( int , long ). 奇怪的是,在一串数字之前出现的一元减号 (减号) 不会将选择减小为( intlong )。 The literal is always positive; 字面量始终为正; the minus sign really is an operator. 减号确实是一个运算符。 So presumably -314159 is exactly the same thing as -((int)314159) . 因此,大概-314159-((int)314159) 完全相同 Except apparently there's a special case to get -2147483648 straight into an int ; 显然,有一种特殊情况可以将-2147483648直接转化为一个int otherwise it'd be -((uint)2147483648) . 否则为-((uint)2147483648) Which I presume does something unpleasant. 我认为这样做会令人不快。

Somehow it seems safe to predict that C# (and friends) will never bother with "squishy name" types for >=128 bit integers. 以某种方式可以肯定地预测C#(和朋友)将永远不会为大于等于128位整数的“ squishy name”类型而烦恼。 We'll get nice support for arbitrarily large integers and super-precise support for UInt128, UInt256, etc. as soon as processors support doing math that wide, and hardly ever use any of it. 一旦处理器支持进行宽泛的数学运算,并且几乎从未使用过它,我们将获得对任意大整数的良好支持以及对UInt128,UInt256等的超精确支持。 64-bit address spaces are really big. 64位地址空间确实很大。 If they're ever too small it'll be for some esoteric reason like ASLR or a more efficient MapReduce or something. 如果它们太小,则出于某种深奥的原因,例如ASLR或更有效的MapReduce或其他原因。

Yes, as Jon said, and unlike the 'C/C++ world', Java and C# aren't dependent on the system they're running on. 是的,正如乔恩所说,与“ C / C ++世界”不同,Java和C#并不依赖于它们所运行的系统。 They have strictly defined lengths for byte/short/int/long and single/double precision floats, equal on every system. 它们对字节/短/整数/长和单精度/双精度浮点数具有严格定义的长度,在每个系统上均相等。

int without suffix can be either 32bit or 64bit, it depends on the value it represents. 不带后缀的int可以是32位或64位,这取决于它表示的值。

as defined in MSDN: 如MSDN中所定义:

When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. 当整数文字没有后缀时,其类型是这些类型中可以表示其值的第一个类型:int,uint,long,ulong。

Here is the address: https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx 这是地址: https : //msdn.microsoft.com/zh-cn/library/5kzh1b5w.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM