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?

- 344,730
- 71
- 640
- 635

- 1,279
- 1
- 14
- 22
-
3Unmanaged code makes the same decision as .NET. In plain C++, sizeof(int) is 4 as well. Think of x64 as extensions to a 32-bit architecture. The default is still 32, you just gain the ability to process 64-bit data *as well*. – jalf Mar 16 '09 at 20:12
-
1@jalf to be more precise, we already had the ability to process 64-bit data, however the x64 CPUs do it faster due to native support. – Mr. TA May 28 '12 at 22:42
-
2Why is this closed? While technically identical, the other question is about Java, this is about .NET / C#. – Christian.K Aug 08 '12 at 10:23
7 Answers
The keyword int
aliases System.Int32
which still requires 4 bytes, even on a 64-bit machine.

- 344,730
- 71
- 640
- 635
-
What about alignment on a CPU word boundary? Even a single `Byte` _may_ be 64-bits wide on x64. – Dai Sep 09 '19 at 08:08
There are various 64-bit data models; Microsoft uses LP64 for .NET: both longs and pointers are 64-bits (although C-style pointers can only be used in C# in unsafe
contexts or as a IntPtr
value which cannot be used for pointer-arithmetic). Contrast this with ILP64 where ints are also 64-bits.
Thus, on all platforms, int
is 32-bits and long
is 64-bits; you can see this in the names of the underlying types System.Int32
and System.Int64
.

- 10,934
- 11
- 59
- 95
-
9You should edit your answer as it is the accepted one to make it completely correct. Remove where you say "although traditional C-style pointers don't exist .NET" and detail System.IntPtr depends on the architecture. – Fernando N. Aug 11 '09 at 16:33
-
2You can use "C-style" pointers in C#, they just have to be done under the unsafe context. Like "fnieto" said, you need to note that IntPtr does depend on the platform, where "sizeof(IntPtr)==4" in x86 & "sizeof(IntPtr)==8" in x64. – zezba9000 Dec 05 '10 at 21:01
int
means Int32
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.
An Int32
is 4 bytes on x86 and x64. An Int64
is 8 bytes either case. The C# int
type is just an alias for System.Int32
. Same under both runtime environments. The only type that does change depending on the runtime environment is an IntPtr
:
unsafe
{
var size = sizeof(IntPtr); // 4 on x86 bit machines. 8 on x64
}

- 19
- 5

- 22,388
- 8
- 62
- 82
-
5Or you could just check IntPtr.Size, which does not require unsafe code. – Jim Mischel Mar 16 '09 at 20:18
-
1
You may be thinking of an int
pointer or System.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 an int
will always correspond to System.Int32
.

- 16,580
- 5
- 54
- 111

- 339,232
- 124
- 596
- 636
Remember int
is just a compiler alias for the basic type Int32
. Given that it should be obvious why int
is only 32 bits on a 64 bit platform.

- 114,645
- 34
- 221
- 317
int i;
int size = BitConverter.GetBytes(i).GetLength(0);
-
3This question was asked 3 years ago and already has an accepted answer. See if you can provide answers to recent questions that do not yet have an accepted answer. – mcknz Feb 15 '13 at 17:12