Showing posts with label Boxing & Unboxing. Show all posts
Showing posts with label Boxing & 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 ( );  
        }    
    }    
}

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

Tuesday, April 19, 2016

C# Datatypes - Datatype Conversion, Boxing & Unboxing

Since C# is a strongly typed language, each variable used in the program should have a type and the data types are declared when we declare the variable. The C# data types are categorized into three types:
  • Value Types:
    • Predefined – int, char, short
    • User-defined types – Structure, Enumerations
  • Reference Types : 
    • Predefined – Objects, Strings
    • User-defined types – Arrays, Classes, Interfaces
  • Pointer Types
  • The variables which are based on value types they directly contains values. But the reference type variables differ from value types as they contain the reference of the object. The value types are derived implicitly from System.ValueType whereas the reference types cannot derive new type from the value type.
    The value type contains the null value and each value type has the implicit default constructor that initialize the default value of that type. Like
    int a = new int ( ); which is same as int a = 0;