types.md - brainchildservices/curriculum GitHub Wiki

Boxing and unboxing


SLIDE-1

  • Boxing and unboxing in C# allows developers to convert .NET data types from value type to reference type and vice versa.

SLIDE-2

  • Converting a value type to a reference type is called boxing in C#.

  • Example:

       				public static void Main()
       					{
       				        int i = 2;  
       				        object o = i; // boxing  							       
       				        Console.WriteLine(i);
       					} 
    

SLIDE-3

  • Converting a reference type to a value type is called unboxing in C#.

  • Example:

       				public static void Main()
       					{
       				        int a = 10;  
       				        object objectOne = a; // boxing  
       				        int b = (int)objectOne; // unboxing  
       				        Console.WriteLine(b);
       					}