How big is an object reference in .NET?

C#.NetReference

C# Problem Overview


What is the size of an object reference in .NET? Does it vary between x86, x64, and/or AnyCPU compilation?

If it makes a difference, I'm interested in C#.

C# Solutions


Solution 1 - C#

The reference itself is basically a pointer. 32 bits on a 32 bit OS, 64 bits on a 64 bit OS.

The size of the object that's referenced is more complicated.

Solution 2 - C#

For determining pointer size, you can use System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)), or sizeof(IntPtr) in unsafe context.

Edit:

Or IntPtr.Size.

Solution 3 - C#

An object reference is basically a pointer to the memory that contains the object's attributes. As such the reference is one processor word in length - 32 bits on 32 bit platforms and 64 bits on x64.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt MillsView Question on Stackoverflow
Solution 1 - C#Samuel NeffView Answer on Stackoverflow
Solution 2 - C#IS4View Answer on Stackoverflow
Solution 3 - C#Igor ZevakaView Answer on Stackoverflow