Introduction
In this article I explained about Boxing and UnBoxing Concept in c#, which is very important. In C# we have Value Types and Reference Types. Value Types are stored in stack and Reference Types are stored in Heap.
Boxing and Unboxing concept related with Value Types and Reference Types. Boxing and unboxing enable value types to be treated as objects.
Converting a Value Type to Reference Type is called Boxing.. converting Reference Type back to the Value Type( the reverse operation of Boxing) is called as UnBoxing which is explicit.
Value Types
Value types are primitive types. All value types are stored in stack and are derived from System.ValueType. All structure and enum types that are derived from System.ValueType are created in stack, so they are called as Value Types.
Reference Types
For Reference Types memory is allocated in heap. All the classes are of reference types . ‘new’ operator in c# returns the memory address to the object.
Boxing
Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap
Let us look a small example
Example1: // Boxing sample int x = 22; Object y = (Object)x; Console.Write(y);
In Boxing there are two types
1. Explicit Boxing 2. Implicit Boxing
The difference between Implicit Boxing and Explicit Boxing is Compiler it self takes care of boxing. You may understand if u look Example2 which contains sample coding in c#
// Implicit boxing int x = 10; object y = x ;
Generally we use Explicit Boxing which is shown in Example1
UnBoxing
As we now Unboxing is the reverse of Boxing. That means we have to convert Reference Type(eg: object) to value Type.
In UnBoxing also there are two types like Boxing
1. Implicit UnBoxing 2. Explicit UnBoxing
Lets now see UnBoxing an object type back to value type. Here is a simple code that unbox an object back to Int32 variable. First we need to box it so that we can unbox.
Example3
int a = 5; // Implicit Boxing
object b = a; //Implicit UnBoxing a = b;
This is a simple example, we are unBoxing the reference type back to value type but, Wait our code is showing some error” Cannot implicitly convert type 'object' to 'int'”. So implicit UnBoxing will not work. you must explicitly convert it into value type
Example4 int a = 5; object b = a; // Implicit Boxing a = (int) b; // Explicit UnBoxing Example 4 Works fine.
Note: The Type the variable uses to Box will remain the same when UnBoxing the same variable. Otherwise you will get System.InvalidCastException
Performance
Performance is less for Boxing and unboxing. these are computationally expensive processes. When a value type is boxed, an entirely new object must be created. This can take up to 20 times longer than an assignment. When unboxing, the casting process can take four times as long as an assignment
Summary
In this article we learned Boxing and Unboxing and also what are Value Types and Reference Types.
About The Author
Mr.Rajendra Kumar Yerra is a Programmer in KST , He Completed His Engineering from JNTU, Hyderabad.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|