Monday, 31 March 2014

5: Type Casting, Boxing and Unboxing in C#

Type Casting:

Type casting help to convert a data type to another data type that belong to the same or a different hierarchy; there are two types of casting.
  1. Implicit
  2. Explicit

1. Implicit type casting

Implicit Type Casting refers to automatic conversion of data types by the C# compiler.
Implicit type casting done only when destination and source data type belong to the same hierarchy.
In implicit type casting, the destination data type must be larger than source data type because in this way we can avoid loss of data.


Example:

int myIntValue = 10;
float myFloatValue = myIntValue; // convert int data type to float data type Implicitly, because both data type belong to same hierarchy “numeric”, destination data type “Float” is larger than integer data type.

Console.WriteLine(myFloatValue);

long myLongValue = myIntValue; //convert int data type to long data type implicitly.

Console.WriteLine(myLongValue);

2. Explicit type casting

Explicit type casting refers to converting of higher precision data type into data type of lower precision.
In this type of casting data might be lose

For Example:

Float à integer conversion
float myFloatValue = 4.5f;// when we initialize float we use f literal as we learn in our second article.
int myIntValue=(int)myFloatValue; //explicit convert float to int
Console.WriteLine(myIntValue);

// result will be 4 becasue we know that integer does not take values after point/decimal place
There are three ways to implement explicit type casting.

Convert Class à provides many methods to convert one data type to another, see example1.
Parse method à parse method convert string to any suitable data type.
ToString method à this method also convert any data type to string without loss of any data.

Example 1:

Float à integer

float myFloatValue = 4.5f;
int myIntValue = Convert.ToInt16(myFloatValue);
Console.WriteLine(myIntValue);
Output:
4

String à character

string myStringValue = "C";
char myCharValue = Convert.ToChar(myStringValue);
Console.WriteLine(myCharValue);
Ouput:
C

You can also use other methods of Convert Class.

Example 2:

String à character

string myStringValue = "C";
char myCharValue =char.Parse(myStringValue);
Console.WriteLine(myCharValue);

Ouput:
C

String à integer

string myStringValue = "125";
int myIntValue = Int16.Parse(myStringValue);
Console.WriteLine(myIntValue);
Ouput:
125

You can also use parse by using other data type like double.Parse etc.

Example 3:

float myFloatValue = 4.5f;
string myStringValue=myFloatValue.ToString();
Console.WriteLine(myStringValue);
Ouput:
4.5

ToString() can also be used with any data type for conversion from any data type to string.

Boxing

Boxing is a process of converting value type to type object (reference type). It wraps the value inside a system.Object Class and store this value to heap.

Example:

Implicit boxing.

int length = 4;
float height = 2.5f;
double area = length * height;
object boxed = area;
Console.WriteLine(boxed);
object boxed = area;

The result of above statement is creating an object reference Boxed, on the stack that references a value of the type double, on the heap.

object is the base class for all value types.

Boxing can also perform explicitly.

int myIntValue = 5;
object o =(object) myIntValue;
Console.WriteLine(o);

Unboxing

Unboxing refers to converting a reference type to a value type,
The source data type (reference type) must match with destination value type when unboxing.

Example 1:

int length = 4;
float height = 2.5f;
double area = length * height;
object boxed = area;
Console.WriteLine("boxing "+boxed);

//unboxing
double newArea=(double)boxed;// here you can see, I explicitly cast boxed to double.
Console.WriteLine("unboxing "+newArea);

Example 2:

This example defines unboxing from object to string.

Unboxing object to string.

object myObject = "Salam Friends";
string myString1 = (string)myObject;
Console.WriteLine(myString1);

Unboxing object to string by ToString() method.

string myString2 = myObject.ToString();
Console.WriteLine(myString2);



No comments:

Post a Comment