Boxing and unboxing are important concepts in C#.
Boxing Vs Unboxing in C#
Boxing and unboxing are important concepts in C#. C# Type System contains three data types: Value Types (int, char, etc.), Reference Types (object) and Pointer Types.
Basically, boxing and unboxing deals in converting a Value Type to a Reference Type, and vice versa.
BOXING |
UNBOXING |
||
It converts value type into an object type. |
It converts an object type into value type. |
||
Boxing is an implicit conversion process. |
Unboxing is the explicit conversion process. |
||
Here, the value stored on the stack is copied to the object stored on the heap memory. |
Here, the object stored on the heap memory is copied to the value stored on the stack . |
||
Example:
Output: Value type of val is 2000 Object type of val is 2019 |
Example:
Output: Value of o is 2019 Value of x is 2019 |
