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 ConversionImplicit 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 ConversionWhere 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. ExampleConsultant cons = new Consultant(); cons.Age = 25; .... Object obj = cons; ProcessData((Employee) obj); NoteThe above code does not give any kind of compiler error, but it throws runtimeInvalidCastException if it is not able to cast to the type.Following is solution to avoid InvalidCastException at runtime because of Explicit conversion.Solution 1Catch the exception and handle it.Consultant cons = new Consultant();
cons.Age = 25;
....
Try
{
ProcessData((Employee) cons);
}
catch(InvalidCastException ex)
{
... Process exception
} Solution 2Make 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.SyntaxType t =expression as type; ExampleEmployee emp = new Employee();
Person p = emp as Person;
if(p!=null)
{ .. Process data }
else
{
.. Display error message or do another code
} Solution 3Make 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.SyntaxType t = expression is type ExampleEmployee 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
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. |
Thoughts on C#, .NET, programming, software development and productivity. Tips and tricks to solve various programming problems.
Tuesday, January 4, 2011
Casting of object and Role of 'is' and 'as' keyword
Labels:
.NET Framework
,
Casting of objects
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment