.NET Boxing and Unboxing

Value types have smaller memory footprint since they are not allocated on the managed heap, and as such does not have the additional fields – sync block index and type handle – and no reference to the field exists. Also not being a reference type, it does not get garbage collected. However, value types can exist on a threads stack or as a field in a managed type on the managed heap.

Sometimes we must convert a value type to a reference type. For this to work, the value type must be boxed. Here is what happens when a value type is boxed:

  1. The size of the new object is calculated. That includes the two overhead members all reference types have. Then memory is allocated on the managed heap.
  2. The value types members are copied to the new managed heap allocated memory.
  3. An address to the new reference type is now returned. The value type is now a reference type.

Unboxing is pretty much the opposite steps:

  1. If the reference to the value type is null, a NullReferenceException is thrown.
  2. If the reference is of the wrong type, an InvalidCastException is thrown.
  3. A pointer to the value type inside the managed object is returned.

When unboxing the cast must be to the correct type.

An unboxing operation is typically followed-up by a copy operation. This copies the fields from the boxed object to the value type.

An C# example of boxing and unboxing. It’s important to be aware that C# automatically emits code to box and unbox objects.

long z = 4;
object o = z;         // boxing
int f = (int)o;       // InvalidCastException
long s = (long)o;     // Unboxing.

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: