Junior (0-2 years)C#

What is boxing and unboxing in C# and why does it affect performance?

Quick answer

Boxing converts a value type to object by allocating it on the heap, and unboxing converts it back; both add allocations and casts, so avoid them in hot code paths.

Assigning an int to an object variable boxes it: the runtime allocates a new object on the heap and copies the value in. Casting back with (int)obj is unboxing, which checks the type and copies the value out. Boxing in a loop creates garbage that the collector has to clean.

Generics were introduced to avoid this: List<int> stores ints directly, whereas the old ArrayList boxed every item. Watch for hidden boxing when a struct is used as an interface type, or when you pass a value type to a method taking object, such as older string.Format overloads.

int i = 42;
object boxed = i;        // boxing: heap allocation
int j = (int)boxed;      // unboxing: type check + copy

var list = new List<int>();   // no boxing
list.Add(i);