Showing posts with label UnBoxing. Show all posts
Showing posts with label UnBoxing. Show all posts

Tuesday, May 3, 2016

UnBoxing

The unboxing is an explicit conversion from the type object to a value or from an interface type to a value that implements interfaces. The unboxing consists of:
  • Checking the object instance to make sure that it is a boxed value of the given value type.
  • Copy the value from the instance variable into the value type variable.
Let us have a look on the example:
using System;

using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication1  
{  
    class unboxing  
    {  
        public static int a
        {  
            get;  
            set;  
        }  
        public static void Main ( )  
        {   
            int i = 10;   
            object j = i;   
   i = (int) j;       //statement for unboxing
Console.WriteLine ("Enter the value: ", a);  
            a= Convert.ToInt32 (Console.ReadLine ( ));  
  
            if (i <= a)  
            {  
                i += a;  
                Console.WriteLine ("\n The sum is: ", i);  
            }  
            else  
            {  
                Console.WriteLine ("\nWrong answer…");  
            }  
            Console.ReadLine ( );  
        }    
    }    
}