Skip to content
David Cumps

Boxing And Unboxing

Part of my 2008 study notes for exam 70-536 (.NET Framework 2.0).

Overview

  • Boxing is an implicit conversion of a value-type to the object type.
    • Boxing a value-type will copy the value on the heap, and place a pointer to the boxed value on the stack.

Good practice

  • Try to prevent boxing/unboxing when possible, use generics or multiple overloads instead.

Examples

Boxing Operation

int i = 123;
object o = (object)i;

Unboxing Operation

object o = 123;
int i = (int)o;

You May Also Like

ArrayList Class

Resizeable, unordered, index-based collection. Suffers from boxing/unboxing when working with value types. Implements IEnumerator, allowing foreach…

BitArray Class

Contains an array of true/false values. Resizable through the Length property. Supports bitwise operations.