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 as we discuss in the Difference section of this post.
CodeProject