What is boxing and unboxing, and why does it matter?
Short answer
Boxing wraps a value type in a heap object (implicit); unboxing extracts it (explicit cast). Both allocate/copy and hurt performance in loops.
Advertisement
AModel answer
Boxing wraps a value type in a heap object (implicit); unboxing extracts it (explicit cast). Both allocate/copy and hurt performance in loops.
int n = 42;
object boxed = n; // boxing
int back = (int)boxed; // unboxingReal-world example
Non-generic ArrayList boxes every int; List<int> avoids it entirely.