Sunday, August 10, 2008

Boxing and Unboxing

Boxing simply means converting a value type to a reference type. Unboxing, on other hand, means converting a reference type to a value type.

For more information about reference types and value types, see this post.

The following is an example of boxing and unboxing:

object MyObject= 2.2; //boxing

double MyNumber=(double)MyObject;//unboxing

 

To improve the performance and maintain type safety, it is recommended to use boxing/unboxing in your code as little as possible .

No comments: