Boxing and Unboxing in C#


Boxing: The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion.
Boxing example:
using System;
class ConversionSamp
{
    static void Main()
    {
        int a = 5;
        Object obj = a;
        Console.WriteLine(a.ToString());
        Console.WriteLine(obj.ToString());
    }
}
Unboxing: The process of converting from a reference type to a value type is called unboxing.
Unboxing example:
using System;
class ConversionSamp
{
    static void Main()
    {
        Object obj = 5;
        int a = (int)obj;
        Console.WriteLine(a.ToString());
        Console.WriteLine(obj.ToString());
    }
}
 
Ebook Download
View all
Learn
View all