Recall & Review
beginner
What is boxing in C#?
Boxing is the process of converting a value type (like int) to an object type. It wraps the value inside an object so it can be treated as a reference type.
Click to reveal answer
beginner
What is unboxing in C#?
Unboxing is the process of extracting the value type from the object. It converts the object back to the original value type.
Click to reveal answer
intermediate
Why does boxing affect performance?
Boxing creates a new object on the heap and copies the value. This uses extra memory and CPU time, so it can slow down the program if done often.
Click to reveal answer
beginner
Show a simple example of boxing and unboxing in C#.
int num = 123; // value type
object obj = num; // boxing
int num2 = (int)obj; // unboxing
Click to reveal answer
intermediate
What happens if you unbox to the wrong type?
An InvalidCastException is thrown at runtime because the object does not contain the expected value type.
Click to reveal answer
What does boxing do in C#?
✗ Incorrect
Boxing wraps a value type inside an object so it can be treated as a reference type.
Which of these is true about unboxing?
✗ Incorrect
Unboxing extracts the original value type from the boxed object.
What error occurs if you unbox to the wrong type?
✗ Incorrect
Unboxing to the wrong type throws an InvalidCastException at runtime.
Why should you avoid excessive boxing?
✗ Incorrect
Boxing creates new objects and copies values, which uses extra memory and CPU time.
Which of these is an example of boxing?
✗ Incorrect
Assigning a value type (int 10) to an object variable causes boxing.
Explain boxing and unboxing in your own words and why they matter in C#.
Think about how value types and objects are different.
You got /3 concepts.
Describe a simple C# code example that shows boxing and unboxing.
Use int and object types.
You got /3 concepts.