What Is Object Type, Boxing, And Unboxing In C#

In this article, I will discuss Object Type, Boxing, and Unboxing in C#.
 
Object Type, Boxing, and Unboxing
 
Object is the ultimate base class of all the classes in .NET Framework. Object type is the root data type that can contain a value of any data type, value type, reference type, user type or predefined. We have to typecast the value before assigning to object type.
 
When we change a value type variable to object type, it is said to be boxing. On the other hand, when we change an object type variable to value type, it is said to be unboxing. 
 
For more understanding, see the example shown below.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         class TutShare  
  8.         {  
  9.             public int value1 = 10;  
  10.             public string value2 = "qwertyuiop";  
  11.             public void Share()  
  12.             {  
  13.                 Console.WriteLine("Inside Share method");  
  14.             }  
  15.             public void UnBoxingExample()  
  16.             {  
  17.                 // This is Boxing as we are converting a 'value type' value into 'object type'   
  18.                 object o = "yeepy";  
  19.   
  20.                 // This is Unboxing as we are converting a 'object type' value into 'value type'   
  21.                 string text = (string)o;  
  22.             }  
  23.         }  
  24.         static void Main(string[] args)  
  25.         {  
  26.             Console.WriteLine("hello Tutpoint");  
  27.   
  28.             // Create Object of class TutShare  
  29.             TutShare tutShare = new TutShare();  
  30.   
  31.             // This is boxing as we are assiging a 'value type' value to 'object type'   
  32.             object obje = 1000;  
  33.             Console.WriteLine(obje);  
  34.   
  35.             //This is another example of boxing  
  36.             obje = tutShare.value1;  
  37.             Console.WriteLine(obje);  
  38.             Console.WriteLine(obje.GetType()); // GetType() is used to get the data type  
  39.   
  40.             // we are assigning value of 'value2' variable to 'obje'  
  41.             obje = tutShare.value2;  
  42.             Console.WriteLine(obje);  
  43.             Console.WriteLine(obje.GetType()); // GetType() is used to get the data type  
  44.   
  45.             object objTutShare = new TutShare();  
  46.             // type casting 'objTutShare' to 'TutShare' type  
  47.             tutShare = (TutShare)objTutShare;  
  48.             tutShare.Share();  
  49.   
  50.             tutShare.UnBoxingExample();  
  51.   
  52.             Console.ReadLine();  
  53.         }  
  54.   
  55.   
  56.     }  
  57.   
  58. }   
The example shown above shows how to perform Boxing and Unboxing and how the object type is used.
 
The output of the above program is -
  • hello Tutpoint
  • 1000
  • 10
  • System.Int32
  • qwertyuiop
  • System.String
  • Inside Share method
Next Recommended Reading Boxing And Unboxing Explained In C#