Skip to main content

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

Mid C#
Quick Answer Boxing wraps a value type (like int) in a heap-allocated object so it can be treated as object. Unboxing extracts it back. The problem: boxing allocates heap memory and adds GC pressure. In tight loops or large collections, this adds up fast. Use generics (List not ArrayList) to avoid boxing.

Answer

Boxing converts a value type into an object by wrapping it inside a heap-allocated reference type.

Unboxing extracts the value type back from the object.

Boxing and unboxing are slow due to heap allocation, GC pressure, and runtime type-checking.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice