0
0
C Sharp (C#)programming~5 mins

Boxing and unboxing execution in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
ADeletes a value type
BConverts an object to a value type
CCopies an object to another object
DConverts a value type to an object
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
What error occurs if you unbox to the wrong type?
AInvalidCastException
BNullReferenceException
CIndexOutOfRangeException
DArgumentException
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
Which of these is an example of boxing?
Astring s = "hello";
Bint x = (int)obj;
Cobject obj = 10;
Dint y = 5 + 3;
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.