When I do sizeof(int)
in my C#.NET project I get a return value of 4. I set the project type to x64, so why does it say 4 instead of 8? Is this because I'm running managed code?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
An
Int32
is 4 bytes on x86 and x64. AnInt64
is 8 bytes either case. The C#int
type is just an alias forSystem.Int32
. Same under both runtime environments. The only type that does change depending on the runtime environment is anIntPtr
:You may be thinking of an
int
pointer orSystem.IntPtr
. This would be 8 bytes on an x64 and 4 bytes on an x86. The size of a pointer shows that you have 64-bit addresses for your memory. (System.IntPtr.Size
== 8 on x64)The meaning of
int
is still 4 bytes whether you are on an x86 or an x64. That is to say that anint
will always correspond toSystem.Int32
.The keyword
int
aliasesSystem.Int32
which still requires 4 bytes, even on a 64-bit machine.Remember
int
is just a compiler alias for the basic typeInt32
. Given that it should be obvious whyint
is only 32 bits on a 64 bit platform.int
meansInt32
in .NET languages. This was done for compatibility between 32- and 64-bit architectures.Here's the table of all the types in C# and what they map to .NET wise.
There are various 64-bit data models; Microsoft uses LP64 for .NET: both longs and pointers are 64-bits (although traditional C-style pointers don't exist .NET). Contrast this with ILP64 where ints are also 64-bits.
Thus, on all platforms,
int
is 32-bits andlong
is 64-bits; you can see this in the names of the underlying types System.Int32 and System.Int64.