18-01-2010, 08:03 PM | #1 |
Member
Join Date: Jan 2010
Posts: 58
Rep Power: 15 |
Boxing and Unboxing in C#
Unboxing is the term used for the conversion of an object into any of the available datatype. So, in case we have an object of a class which has just one data member defined, with a few functions to use it, we can convert the object to the type of the data member. Boxing in C# - C# has a very good advantage in this regard, over other languages. In C#, everything is an object. So, when we declare an integer, an object of the integer class (classes for the default datatypes are pre-defined in C# under System namespace), is created. That means, there is essentially no such thing as Boxing in C#. For example, check out this piece of code. Code:
Console.WriteLine (1234.ToString()); Unboxing in C# - Unboxing in C# is not as straightforward as boxing. We have to determine the datatype that we want the object to be converted to. Consider this code for better understanding. Code:
int a = 123; object myobject = num1; //boxing int num2 = (int) myobject; //unboxing |
Bookmarks |
Tags |
boxing, unboxing |
|
|