Tuesday, January 4, 2011

Casting of object and Role of 'is' and 'as' keyword

In this post, I am going to discuss about the casting of object form one type to another type and what the
Things required to be kept in mind when casting object.

Casting basically takes place when we are relating between classes I.e. INHERITANCE. Classes have
A parent child relationship, I.e. Is-a relationship between them. To understand it, take an example of the
Below diagram:
As show in the above image, Employee and Manager are having is-a relationship with person
, I.e., inherited from person class.Now I have one method in my main class which takes
Employee as argument and does processing on the property of the class.
Public void ProcessData(Person p)
{
....process data
Console.WriteLine(p.ToString());
}

Implicit Conversion

Implicit conversion takes place when there is an is-a relation between two classes, I.e., inherited from other.
As per the diagram,
Employee is Inherited from Person class and so that as per OOD rule, there is
No need to convert
Employee class to Person class.

Example:

Public void testdata()
{
Employee emp = new Employee();
emp.Age = 25;
emp.BirthDate = DateTime.Now;
emp.Height = "5 ' 3";
emp.salary = 2000;
emp.type = 1;
ProcessData(emp);
}
When you compile and run the code, there is no error because it is Implicit conversion from child to parent.

Explicit Conversion

Where there is no relation between classes and if you want to pass the object of another type to method,
Then you need to convert it explicitly.

Example

Consultant cons = new Consultant();  cons.Age = 25;  ....
Object obj = cons;  ProcessData((Employee) obj);

Note

The above code does not give any kind of compiler error, but it throws runtime
InvalidCastException if it is not able to cast to the type.
Following is solution to avoid InvalidCastException at runtime because of Explicit conversion.

Solution 1

Catch the exception and handle it.
Consultant cons = new Consultant();
cons.Age = 25;  
....
Try
{
ProcessData((Employee) cons);  
}
catch(InvalidCastException ex)
{
... Process exception
}

Solution 2

Make use of 'as' keyword of C#. It does conversion from one object to another object and returns Null
If it is not able to convert. Use it when you want to convert object from one type to another object.

Syntax

Type t =expression as type;

Example

Employee emp = new Employee();
Person p = emp as Person;
if(p!=null)
{    .. Process data   }
else
   {
.. Display error message or do another code
}

Solution 3

Make use of 'is' keyword of C#. It does conversion from one object to another object and returns
False if it is not able to convert. Use it when you want to check whether it is convertible or not.

Syntax

Type t = expression is type

Example

Employee emp = new Employee();
if(emp is Person)
{
Person p = (Person)emp;
.. Process data
}
else
   {
.. Display error message or do another code
}

Difference between as and is

  • As operator does conversion form one type to another type and returns Null if conversion fails.
    There is no need for conversion again if it's convertible as shown in the example code.

  • Is operator checks whether one object is convertible to another type or not and returns false
    If not. So you need to convert object to base type if it is convertible as shown in the example code.

Summary

'is' and 'as' keywords play an important role when we do the casting of the related objects explicitly. But you need to use it according to the situation.

No comments :

Post a Comment