What is Boxing and Unboxing in C# ?

What is Boxing and Unboxing in C# ?


Boxing


Definition : the process of converting a value type to the object type or any interface type implemented by this value type

Boxing is implicit


Example

int i = 10;

object o = i; //performs boxing


Above example ,

The integer variable i is assigned to object o. 

This process of converting int to object is called boxing.



Unboxing


Definition: Unboxing is the reverse of boxing

It is the process of converting a reference type to a value type

Unboxing extracts the value of the reference type and assigns it to a value type.


Example

 

object o = 10;

int i = (int)o;



Post a Comment (0)
Previous Post Next Post