Boxing and unboxing in C#

By: Ram Baskar Viewed: 169 times  Printer Friendly Format    


Boxing is the operation of converting a value of a value type into a value of a corresponding reference type. Boxing in C# is implicit.

Unboxing is the operation of converting a value of a reference type (previously boxed) into a value of a value type. Unboxing in C# requires an explicit type cast. A boxed object of type T can only be unboxed to a T (or a nullable T).

Example:
int foo = 42; // Value type.
object bar = foo; // foo is boxed to bar.
int foo2 = (int)bar; // Unboxed back to value type.


Most Viewed Articles (in Csharp )

Latest Articles (in Csharp)

Comment on this tutorial