Monday 2 October 2017

Boxing and Unboxing

Boxing- Implicit conversion of a value type (int, char, etc) to a reference type (object) is known as BOXING.

In boxing process, a value type is being allocated on the heap rather than the stack.


Unboxing- Explicit conversion of reference type (object) to a value type is known as UNBOXING.

In unboxing process, boxed value type is unboxed ffrom the heap and assigned to a value type which is being alloccated on the stack.

example: int stackvar = 15;
                Object boxedvar = stackvar; //Boxing = int is created on the heap (reference type)
                int Unboxed = (int) boxedvar; // Unboxing = boxed int is unboxed from the heap
                                                                 // and assigned to an int stack variable.      

example: int i = 5;
                Arraylist arr = new Arraylist(); 
                // Arraylist contains object type value.
                arr.Add(i);n //Boxing occurs automatically.
                int j = (int) arr[0]; //Unboxing occurs.

Issues with boxing and unboxing:

  • Sometime boxing is necessary, but avoid it if possible, since it slow down the performance and increase memory requirements.
  • Attempting to unbox a null causes a NullreferenceException.
       example:  int? stackvar = null;
                       object boxedvar = stackvar;
                       //NullReferenceException
                        int unboxed = (int) boxedvar;
                       // Object reference not set to an instance of an object.

  • Attempting to unbox a reference to an incompatible value type causes an InvalidCastException.
       example:  int stackvar = 15;
                       object boxedvar = stackvar;
                       // InvalidCastException
                       float unboxed = (float) boxedvar; // Specified cast is not valid;

Difference between Value type and Reference type


Difference between stack and heap memory


Reference Type & Value Type


  • The types are either treated by value type or by reference type.
  • Reference type variables are stored in the heap while value type variables are stored in the stack.
  • A value type holds the data within its own memory allocation and a reference type contains a pointer to another memory location that holds the real data.


Value Type:

  • When you created a value type, a single space in the memory is allocated to store the value and that variable directly holds a value.
  • If you assign it to another variable, the value is copied directly and both variables work independently.
  • Value type can  be created at compile time and stored in stack memory, because of this, garbage collector can't access the stack.
  • All the values are derived implicitly from the System.ValueType.
  • You cannot derive a new type from a value type. However, like reference types, structs can implement interfaces.
  • eg:- int X = 25;
  • Here the value 25 is stored in an area of memory called the stack.
  • When the variable X goes out of scopebecause the method in which it was defined has finished executing, the value is discarded from stack.
  • Using the stack is efficient, but the limited lifetime of value types makes them less suited for sharing data between different classes.
  • Each value type has an implicit default constructor that initialize the default value of that type.
  • Value type cannot contain the null value.
  • The nullable types features does allow for value types to be assigned to null.

Reference Type:

  • Reference types represent the address of the variable rather than the data itself.
  • Assigning a reference variable to another doesn't copy the data. Instead it created a second copy of the reference, which refers to the same location of the heap as the original value.
  • When a reference type variable is no longer used, it can be reclaimed by garbage collector.
  • Reference type is created at run time.
  • eg: Classes, objects, arrays, indexers, interfaces, etc.