Sunday, 11 October 2015

Boxing and Unboxing in C#

using System;
class Program
{
    public static void Main()
    {
        int i = 20;

        // Boxing is the process of converting value types to reference type and its a implicit process
        object obj = i;
       
        //Unboxing is the process of converting a reference type back to value type and its a explicit process
        int j = (int)obj;

        Console.WriteLine("The value of i = {0}", i);
        Console.WriteLine("The value of obj = {0}", obj);
        Console.WriteLine("The value of j = {0}",j);
    }
}
/* Output:
    The value of i = 20
    The value of obj = 20
    The value of j = 20
*/

No comments:

Post a Comment