Convertion Type Boxing and UnBoxing in C#

Boxing an Unboxing in C#

Note:- These all are type convertion.
            
Boxing:- Any  value  of value type converted to object type .
          
                ex:-
                                 int i = 1; 
                                 object obj = i; // Boxing

UnBoxing:-    object  type converted to value  type .
          
                ex:-
                                 int i = 1; 
                                 object obj = i; 
                                 int j = (int)obj; //Unboxing.




Program:- 

using System;
using System.Text;


namespace Test_Application
 {
    class Program
     {       
         static void Main(string[] args)
         {
            int i = 1; 
            object obj = i; // Boxing  :-converting value type to Objective type..
            Console.WriteLine("Boxing value "+obj);
            int j = (int)obj; // UnBoxing :- converting object type to value type.
            Console.WriteLine("UnBoxing value "+j); 
            Console.ReadLine();
        } 
    }
 }

Output :-

         Boxing value 1
         UnBoxing value 1

Share this

Previous
Next Post »