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#?
ADeletes a value type
BConverts an object to a value type
CCopies an object to another object
DConverts a value type to an object
✗ 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?
AIt converts a value type to a string
BIt converts an object back to a value type
CIt creates a new object on the heap
DIt copies a value type to another value type
✗ Incorrect
Unboxing extracts the original value type from the boxed object.
What error occurs if you unbox to the wrong type?
AInvalidCastException
BNullReferenceException
CIndexOutOfRangeException
DArgumentException
✗ Incorrect
Unboxing to the wrong type throws an InvalidCastException at runtime.
Why should you avoid excessive boxing?
AIt slows down performance and uses more memory
BIt causes syntax errors
CIt deletes variables
DIt makes code unreadable
✗ Incorrect
Boxing creates new objects and copies values, which uses extra memory and CPU time.
Which of these is an example of boxing?
Astring s = "hello";
Bint x = (int)obj;
Cobject obj = 10;
Dint y = 5 + 3;
✗ 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.
Practice
(1/5)
1. What happens during boxing in C#?
easy
A. A value type is converted to an object type.
B. An object type is converted to a value type.
C. A reference type is converted to a value type.
D. A value type is converted to a string.
Solution
Step 1: Understand boxing concept
Boxing means wrapping a value type (like int) inside an object so it can be treated as an object.
Step 2: Identify the conversion direction
Boxing converts a value type to an object type, not the other way around.
Final Answer:
A value type is converted to an object type. -> Option A
Quick Check:
Boxing = value type to object [OK]
Hint: Boxing wraps value types as objects [OK]
Common Mistakes:
Confusing boxing with unboxing
Thinking boxing converts to string
Mixing up value and reference types
2. Which of the following is the correct syntax to unbox an object to an int in C#?
easy
A. int x = obj.ToInt();
B. int x = obj as int;
C. int x = Convert.ToInt32(obj);
D. int x = (int)obj;
Solution
Step 1: Recall unboxing syntax
Unboxing requires an explicit cast from object to the original value type.
Step 2: Check each option
int x = (int)obj; uses explicit cast (int)obj which is correct. int x = obj as int; uses 'as' which works only for reference types. int x = Convert.ToInt32(obj); converts but is not unboxing. int x = obj.ToInt(); is invalid syntax.
Final Answer:
int x = (int)obj; -> Option D
Quick Check:
Unboxing = explicit cast (int)obj [OK]
Hint: Unboxing needs explicit cast (int)obj [OK]
Common Mistakes:
Using 'as' keyword for unboxing
Using Convert.ToInt32 instead of cast
Calling non-existent methods like ToInt()
3. What is the output of the following C# code?
int a = 10; object obj = a; // boxing int b = (int)obj; // unboxing b += 5; Console.WriteLine(a);
medium
A. 15
B. 10
C. 5
D. Compilation error
Solution
Step 1: Understand boxing and unboxing here
Variable 'a' is boxed into 'obj'. Then 'obj' is unboxed into 'b'. 'b' is a separate copy.
Step 2: Check value changes
Incrementing 'b' by 5 does not affect 'a'. So 'a' remains 10.
Final Answer:
10 -> Option B
Quick Check:
Boxing copies value; original stays 10 [OK]
Hint: Boxing copies value; original unchanged [OK]
Common Mistakes:
Assuming 'a' changes when 'b' changes
Confusing reference and value types
Thinking boxing links variables
4. Identify the error in this C# code snippet:
object obj = 123; long l = (long)obj; Console.WriteLine(l);
medium
A. Invalid cast exception at runtime
B. Compilation error due to missing cast
C. No error, prints 123
D. Null reference exception
Solution
Step 1: Analyze boxing and unboxing types
Boxing stores an int (123) in obj. Unboxing must cast back to int, not long.
Step 2: Understand runtime behavior
Trying to unbox as long causes InvalidCastException because obj holds int, not long.
Final Answer:
Invalid cast exception at runtime -> Option A
Quick Check:
Unboxing type mismatch causes runtime error [OK]
Hint: Unbox only to original value type [OK]
Common Mistakes:
Casting to different value type than boxed
Expecting compile-time error instead of runtime
Ignoring boxing type stored
5. Given a list of integers boxed as objects, how can you sum their values correctly in C#?
List<object> list = new List<object> { 1, 2, 3, 4 };
Which code snippet correctly sums the integers?
hard
A. int sum = 0; foreach (int x in list) sum += x;
B. int sum = 0; foreach (object o in list) sum += (o as int);
C. int sum = 0; foreach (object o in list) sum += (int)o;
D. int sum = list.Sum();
Solution
Step 1: Understand list content and types
List contains objects boxed from ints. To sum, unbox each object to int.
Step 2: Evaluate each option
A: int sum = 0; foreach (int x in list) sum += x; compile error - cannot implicitly convert object to int. B: int sum = 0; foreach (object o in list) sum += (o as int); compile error - 'as' cannot be used with non-nullable value types. C: int sum = list.Sum(); fails because list is List<object>, no Sum() overload. D: int sum = 0; foreach (object o in list) sum += (int)o; unboxes correctly with (int)o.
Final Answer:
int sum = 0; foreach (object o in list) sum += (int)o; -> Option C
Quick Check:
Unbox each object with (int)o to sum [OK]
Hint: Unbox each object with (int)o before summing [OK]
Common Mistakes:
Trying to foreach as int directly
Using Sum() on List<object>
Using 'as' operator for unboxing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.