Thursday, May 27, 2010

What is the Difference between Boxing and Unboxing

Boxing :- is Converting Value type to Reference type.
Unboxing :- is Converting Reference type to Value type

Example on Boxing and UnBoxing

namespace Boxing_vs_Unboxing
{
class Program
{
static void Main(string[] args)
{
int i = 1;
object obj = i;//Boxing
Console.WriteLine(obj.ToString());
int j = (int)obj;//Unboxing
Console.WriteLine(i.ToString());
Console.Read();
}
}
}

0 comments:

Post a Comment