Boxing and Unboxing in C# (Interview Guide)

Most developers say:
“It converts value types to reference types.”
That’s correct — but misses the performance impact.

What is boxing and unboxing?

❌ Bad answer

It converts value types to reference types.

✅ Senior answer

Boxing converts a value type into an object, causing heap allocation. Unboxing extracts the value type back. Excessive boxing/unboxing can hurt performance due to allocations and casting, especially in high-frequency operations.

How it works

Example

int x = 10;
object obj = x;      // Boxing

int y = (int)obj;    // Unboxing

Why it matters (Performance)

Boxing creates heap allocations → more GC pressure → slower performance in loops or high-load systems.

Common mistakes

❌ Using object unnecessarily

Causes implicit boxing without realizing.

❌ Boxing in loops

Repeated allocations can destroy performance.

Want to understand real .NET performance traps?

Practice Now
← See all .NET interview questions