Monday, April 2, 2018

Boxing, Unboxing And Casting

Boxing is the concept of converting value type to reference type and unboxing is to convert reference type to value type. Boxing is implicit but we need to explicitly unbox the variables with the help of casting concept.

 double x = 123.45d;  
 object obj = x;                       // boxing  
 double y = (double)obj;               // unboxing  
But now you might be thinking about actual why we need to use boxing and unboxing and why we need to use like this kind of programming.

The answer of this question is suppose you’re using a library which has a method like,

 public static object CalculateSimpleInterest(ref object principalAmount)  
 {  
      return (2 * 5 * (double)principalAmount) / 100;  
 }  
And now you want to use it then the value whatever you’ve you need to box it firstly and then unbox the results. And we already know that boxing is implicit but unboxing is explicit.

 double x = 125600.876d;  
 object obj = x;                                                       // boxing  
 object SimpleInterest = (double)CalculateSimpleInterest(ref obj);     // unboxing  
But remember that boxing and unboxing both has its own drawbacks, they have performance issues. So we just need to perform boxing and unboxing only when we really need them otherwise we should avoid from them.

Casting

Casting is actually all about to change the type of the variable and convert it into some other type which we want. Here we’ve 2 types of casting.
  • ·        Implicit type casting
  • ·        Explicit type casting
As we know that a small bucket water can be poured into the bigger bucket. Similarly, we know different types of numerical and fractional buckets. So if we try to place the value of smaller numerical bucket (int) into the bigger numerical bucket then C# compiler automatically do it for us just by assigning the values implicitly. But if we’re putting the value of the bigger container into the smaller container then we need to make sure our own and tell the compiler that it is ok, the bigger container has small value which can be put into the smaller container otherwise the value will be overflow from the small container and data might be lost.
But if long container has a bigger value which can’t assign into the int then C# compiler will generate the error because this type of problem might loss the data during the conversion and it is difficult to catch such kind of error.

Similarly if we want to convert the fractional type value into the numerical then we need to explicitly tell the compiler to allow him for conversion and in result this conversion will lost the fractional value of floating number.
Another noticeable thing is if we cast the fractional number into numerical one then it is ok. But if we cast the fractional number into ‘object’ and then try to cast this dynamic object into numerical container then we’ll see the error because we can’t directly cast the fractional reference type value into numerical type.

 double num = 2.12454d;  
 int num1 = (int) num;                          // it is ok  
But if we try to do it,

 double num = 2.12454d;  
 object obj = num;  
 int num1 = (int) obj;                          // runtime exception  
To resolve this kind of issue,

 double num = 2.12454d;  
 object obj = num;  
 int num1 = (int)(double) obj;  
First we’ve to explicitly unbox ‘obj’ into double and then cast it into integer. This is how it works.

0 comments:

Post a Comment