Is an int a 64

In my C# source code I may have declared integers as:

int i = 5;

or

Int32 i = 5;

In the currently prevalent 32-bit world they are equivalent. However, as we move into a 64-bit world, am I correct in saying that the following will become the same?

int i = 5;
Int64 i = 5;

No. The C# specification rigidly defines that int is an alias for System.Int32 with exactly 32 bits. 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. To the specification:

CLI specification section 8.2.2 (Built-in value and reference types) has a table with the following:

  • System.Int32 - Signed 32-bit integer
  • C# specification section 8.2.1 (Predefined types) has a similar table:

  • int - 32-bit signed integral type
  • This guarantees that both System.Int32 in CLR and int in C# will always be 32-bit.


    Will sizeof(testInt) ever be 8?

    No, sizeof(testInt) is an error. testInt is a local variable. The sizeof operator requires a type as its argument. This will never be 8 because it will always be an error.

    VS2010 compiles ac# managed integer as 4 bytes, even on a 64 bit machine.

    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; it is just as if you'd said "4" in the source code.

    Does anyone know if/when the time will come that a standard "int" in C# will be 64 bits?

    Never. Section 4.1.4 of the C# specification states that "int" is a synonym for "System.Int32".

    If what you want is a "pointer-sized integer" then use IntPtr. An IntPtr changes its size on different architectures.

    链接地址: http://www.djcxy.com/p/21048.html

    上一篇: 如果Int32只是int的别名,那么Int32类如何使用int?

    下一篇: 是一个int 64