Simple Code in C# to demonstrate Boxing and unBoxing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Postslush
{
    class TestBoxingbypostslush
    {
        public static void Main()
 {
            int i = 123;
            object obj = i;  // Implicit boxing

            Console.WriteLine("\nThe value-type value = {0}", i);
            Console.WriteLine("\nThe object-type value = {0}", obj);
            Console.ReadLine();
            i = 456;       // Change the contents of i  
            Console.WriteLine("\nThe value-type value = {0}", i);
            Console.WriteLine("\nThe object-type value = {0}", obj);
            Console.ReadLine();

            int j = (int)obj;
            Console.WriteLine("\nThe value-type value = {0}", j);
            Console.ReadLine();
        }
    }
}
Output :


Previous Post Next Post