Difference between Boxing and Unboxing in .NET
π‘ Concept Name
Boxing and Unboxing
π Quick Intro
Boxing is the process of converting a value type (like int) to an object (reference type). Unboxing is the reverseβextracting the value type from the object. This mechanism allows value types to be treated like objects.
π§ Analogy / Short Story
Think of boxing like wrapping a gift (value type) into a box (object) so it can be stored anywhere. Unboxing is opening that gift and taking out the actual item (value) again. It adds flexibility but takes effort (performance).
π§ Technical Explanation
- Boxing wraps a value type in a reference type wrapper (object or interface).
- Unboxing extracts the value type from the object by explicit casting.
- Boxing allocates memory on the heap; unboxing retrieves the value to the stack.
- Both operations are costly compared to simple assignments.
π― Purpose & Use Case
- β Passing value types to methods that accept objects
- β
Storing value types in collections like
ArrayList
(non-generic) - β Legacy support where type generalization is needed
π» Real Code Example
// Boxing
int x = 42;
object obj = x; // boxing happens
// Unboxing
int y = (int)obj; // unboxing happens
Console.WriteLine($""Original: {x}, Boxed: {obj}, Unboxed: {y}"");

β Interview Q&A
Q1: What is boxing?
A: Converting a value type to an object type.
Q2: What is unboxing?
A: Converting an object back to its value type.
Q3: Where is memory allocated during boxing?
A: On the heap.
Q4: Is boxing implicit or explicit?
A: Boxing is implicit.
Q5: Is unboxing implicit or explicit?
A: Unboxing is explicit.
Q6: What happens if unboxing fails?
A: An InvalidCastException
is thrown.
Q7: Is boxing performance efficient?
A: No, it has performance overhead.
Q8: Which collections require boxing in .NET Framework?
A: ArrayList
, Hashtable
(non-generic)
Q9: How can we avoid boxing in .NET?
A: Use generics.
Q10: Can structs be boxed?
A: Yes, structs are value types and can be boxed.
π MCQs
Q1. What is boxing in .NET?
- Converting string to char
- Converting object to int
- Converting value type to reference type
- Converting reference to array
Q2. Which type of memory is used during boxing?
- Stack
- Heap
- CPU
- Cache
Q3. Which operation is implicit?
- Unboxing
- Boxing
- Casting
- Conversion
Q4. Which operation is explicit in C#?
- Boxing
- Parsing
- Unboxing
- Initialization
Q5. What type of exception is thrown during invalid unboxing?
- NullReferenceException
- StackOverflowException
- InvalidCastException
- TypeMismatchException
Q6. How can boxing be avoided in .NET?
- Use interfaces
- Use base classes
- Use generics
- Avoid arrays
Q7. What is unboxing?
- Extracting string from char
- Extracting object from array
- Extracting value type from object
- Extracting array from object
Q8. Which data structure causes boxing in .NET Framework?
- List<int>
- ArrayList
- Dictionary<int,int>
- LinkedList<T>
Q9. What type of types can be boxed?
- Reference Types
- Only int
- Only string
- Value Types
Q10. Why should boxing/unboxing be minimized?
- It reduces security
- It increases compile time
- It causes performance overhead
- It makes code unreadable
π‘ Bonus Insight
Always prefer generics (like List<int>
) over non-generic collections to avoid unnecessary boxing. Avoid using object
unless flexibility truly requires it.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!