C# Simplified - Boxing and Unboxing
(Page 4 of 5 )
Boxing is the conversion of a value type into a reference type. Consider the listing given below:
Listing 1.5
// Value type X defined
int X = 600;
// X boxed to a object type which is reference based
object Y = X;
On the other hand, Unboxing is the conversion of a reference type into a value type. In listing 1.6, Y can be unboxed to a value type as shown below:
Listing 1.6
// Y unboxed to an integer type
int Z = (int)Y
The only difference is that an explicit cast is required while converting a reference type into a value type as shown above.
Next: Classes >>
More C# Articles
More By Anand Narayanaswamy