What is Boxing and Unboxing in C#?
π‘ Concept: Boxing and Unboxing
Boxing is the process of converting a value type to an object type, and Unboxing is the reverseβextracting the value type from the object. These operations allow value types to behave like reference types.
π Quick Intro
Boxing and Unboxing bridge the gap between value types and reference types in C#. When a value type is assigned to a variable of type object, it's boxed. Later, when that object is converted back to a value type, it's unboxed.
π§ Analogy
Imagine boxing as putting an apple (value type) inside a box (object). You can pass the box around without worrying about whatβs inside. Unboxing is opening the box and retrieving the apple. If you expect a banana and get an apple, youβll face a type error!
π§ Technical Explanation
- Boxing copies the value type data and wraps it inside a System.Object.
- Unboxing involves explicit casting from object to the original value type.
- Boxing has performance overhead due to heap allocation and garbage collection.
- Incorrect unboxing (wrong type) throws an InvalidCastException.
- Boxing is implicit, unboxing must be explicit.
π― Use Cases
- When storing value types in non-generic collections like ArrayList.
- When passing value types as parameters to methods expecting object.
- In reflection and serialization scenarios.
π» Code Example
int x = 42;
object boxed = x; // Boxing
int y = (int)boxed; // Unboxing
Console.WriteLine(boxed); // Outputs 42
Console.WriteLine(y); // Outputs 42

β Interview Q&A
Q1: What is boxing in C#?
A: It is the conversion of a value type to an object.
Q2: What is unboxing?
A: Extracting the value type from an object.
Q3: Is boxing implicit or explicit?
A: Boxing is implicit; unboxing is explicit.
Q4: What happens if you unbox to the wrong type?
A: It throws an InvalidCastException.
Q5: Does boxing affect performance?
A: Yes, due to heap allocation and GC overhead.
Q6: Is boxing needed in generic collections?
A: No, generics avoid boxing by maintaining type safety.
Q7: Can you override boxed object behavior?
A: You can override ToString(), Equals(), and GetHashCode().
Q8: Can structs be boxed?
A: Yes, structs are value types and can be boxed.
Q9: Can you cast boxed int to long?
A: No, it causes a runtime error. Cast it to int first.
Q10: Where does the boxed object get stored?
A: In the managed heap.
π MCQs
Q1. What is boxing in C#?
- Converting object to value type
- Storing variables
- Converting value type to object
- None of the above
Q2. What is required for unboxing?
- Implicit cast
- No cast
- Explicit cast
- String conversion
Q3. What type is used to box a value?
- String
- Dynamic
- Object
- Class
Q4. Where is boxed data stored?
- Stack
- Heap
- Register
- Cache
Q5. Does boxing affect performance?
- No
- Yes
- Never
- Depends on compiler
Q6. What happens if unboxed to incorrect type?
- Compile error
- StackOverflow
- InvalidCastException
- NullReference
Q7. Which collection causes boxing?
- List<int>
- ArrayList
- HashSet<int>
- Queue<int>
Q8. Which conversion is unboxing?
- int to object
- object to int
- string to object
- long to object
Q9. Boxing wraps value in?
- Pointer
- Reference
- Object instance
- Byte array
Q10. Which is NOT a boxing example?
- object o = 10;
- object o = true;
- string name = "John";
- object o = 'A';
π‘ Bonus Insight
Avoid unnecessary boxing in performance-critical applications. Use generics where possible to ensure type safety and avoid boxing overhead.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!