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 ( );  
        }    
    }    
}

Monday, May 2, 2016

Boxing

Boxing is a process of converting a value type to the object type or to any other interface type which is implemented by this value type. When CLR boxes a value type it wraps the value inside a System.Object and stores it on the managed heap. Boxing is done implicitly. It is used to store the value types in the garbage collected heap. It allocates an object instance on the heap and copies the value into the new object. For example:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication1  
{  
    class boxing  
    {  
public static int a
        {  
            get;  
            set;  
        }  
        public static void Main ( )  
        {   
            int i = 10;   
            object j = i;   //statement for boxing
  
            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 ( );  
        }    
    }    
} 

Sunday, May 1, 2016

Float to int Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 float a = 70.0f;
 int b = Convert.ToInt32(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 70

String to int Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 string a = "97";
 int b = Convert.ToInt32(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 97