Programming Tutorials

Boxing and unboxing in C#

By: Ram Baskar in Csharp Tutorials on 2011-02-05  

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.





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Csharp )

Latest Articles (in Csharp)