Google News
logo
C# - Interview Questions
What is Boxing and Unboxing in C#?
Boxing and Unboxing both are used for type conversions.
 
The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion. Here is an example of boxing in C#.
// Boxing  
int anum = 123;  
Object obj = anum;  
Console.WriteLine(anum);  
Console.WriteLine(obj); ​

 

The process of converting from a reference type to a value type is called unboxing. Here is an example of unboxing in C#.
// Unboxing  
Object obj2 = 123;  
int anum2 = (int)obj;  
Console.WriteLine(anum2);  
Console.WriteLine(obj); 
Advertisement