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

Boxing and unboxing execution in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Boxing and unboxing execution
Value type variable
Boxing: Wrap value in object
Reference type variable holds boxed value
Unboxing: Extract value from object
Value type variable restored
Boxing wraps a value type into an object; unboxing extracts the value back from the object.
Execution Sample
C Sharp (C#)
int x = 123;
object o = x; // boxing
int y = (int)o; // unboxing
Console.WriteLine(y);
This code boxes an int into an object, then unboxes it back to int and prints it.
Execution Table
StepActionVariableValue/StateNotes
1Declare int xx123x holds value type 123
2Box x into object ooBoxed 123o holds reference to boxed int 123
3Unbox o to int yy123y gets value extracted from boxed object
4Print yOutput123Output shows unboxed value
5End--Execution ends
💡 Program ends after printing the unboxed value 123
Variable Tracker
VariableStartAfter BoxingAfter UnboxingFinal
x123123123123
onullBoxed 123Boxed 123Boxed 123
yundefinedundefined123123
Key Moments - 3 Insights
Why does boxing create a new object instead of just copying the value?
Boxing wraps the value type inside a reference type object on the heap, so it must create a new object to hold the value separately, as shown in step 2 of the execution_table.
What happens if you try to unbox to the wrong type?
Unboxing to the wrong type causes an InvalidCastException at runtime because the object holds a boxed value of a specific type, as implied in step 3 where unboxing must match the original type.
Does the original value type variable change during boxing or unboxing?
No, the original value type variable remains unchanged throughout, as seen in the variable_tracker where 'x' stays 123 after boxing and unboxing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'o' after step 2?
Anull
B123
CBoxed 123
Dundefined
💡 Hint
Check the 'Value/State' column for variable 'o' at step 2 in execution_table.
At which step does the variable 'y' get its value assigned?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Variable' columns in execution_table to see when 'y' is assigned.
If we skip boxing and assign 'x' directly to 'o' without casting, what would happen?
ACompile error
BBoxing still happens automatically
CUnboxing happens instead
Do becomes null
💡 Hint
In C#, assigning a value type to an object variable causes boxing automatically, as shown in step 2.
Concept Snapshot
Boxing: Convert value type to object (reference type).
Unboxing: Extract value type from boxed object.
Boxing creates a new object on the heap.
Unboxing requires explicit cast to original type.
Incorrect unboxing causes runtime error.
Original value type variable stays unchanged.
Full Transcript
This example shows boxing and unboxing in C#. First, an int variable x is declared with value 123. Then x is boxed into an object o, which means a new object is created on the heap holding the value 123. Next, the object o is unboxed back to an int y by casting. Finally, y is printed, showing 123. The original variable x remains unchanged throughout. Boxing wraps the value type into a reference type object, and unboxing extracts the value back. Unboxing must match the original type or it causes an error. This trace helps visualize how values move between value and reference types in C#.