Please enable Javascript to correctly display the contents on Dot Net Tricks!

Safe Type Casting with IS and AS Operator

  Author : Shailendra Chauhan
Posted On : 29 Dec 2012
Total Views : 92,347   
Updated On : 12 Oct 2013
 

Type Casting is the mechanism to convert one data type to another. While type casting of one data type to another, we get exception if the previous one data type is not compatible with the new data type. To avoid this exception, we have IS and AS operator in C# for safe type casting. Let's understand how to use both of them.

IS Operator

The IS operator checks whether the type of an given object is compatible with the new object type. It returns boolean type value : true if given object is compatible with new one, else false. In this way IS operator help you to do safe type casting.

How to do it..

Object obj = new Object(); // Creates a new Object obj
// checking compatibility of obj object with other type
Boolean b1 = (obj is Object); // b1 is set to true.
Boolean b2 = (obj is Employee); // The cast fails: no exception is thrown, but b2 is set to false.
//we can also use it 
if (obj is Employee) 
{
 Employee emp = (Employee) obj;
 // TO DO:
}

Note

  1. If the reference of the given object is null, the IS operator will return false since there is no object available to check its type.

In this way, CLR is checking the obj object type twice. First time with in the if condition and if it is true, with in the if block. Actually this way affect the performance since each and every time CLR will walk the inheritance hierarchy, checking each base type against the specified type (Employee). To avoid this we have AS operator.

AS Operator

The AS operator also checks whether the type of an given object is compatible with the new object type. It returns non-null if given object is compatible with new one, else null. In this way AS operator help you to do safe type casting. The above code can be re-written by using AS operator in a better way.

How to do it..

Object obj = new Object(); // Creates a new Object obj
// checking compatibility of obj object with other type
Employee emp = obj as Employee; // The cast fails: no exception is thrown, but emp is set to null.
if (emp != null) 
{
 // TO:DO
}

Note

  1. If the reference of the given object is null, the AS operator will return NULL since there is no object available to check its type.

  2. AS operator performs only reference conversions, nullable conversions, and boxing conversions. This operator cannot perform other conversions like as user-defined conversions.

In this way, CLR is checking the obj object type only one time. Hence AS operator provide good performance over IS operator. Now if you want to use emp object then it will throw NullReferenceException as given below.

Object obj = new Object();
Employee emp = obj as Employee; // try to Cast obj to an Employee
// The above cast fails: no exception is thrown, but emp is set to null.
emp.ToString(); // Accessing emp throws a NullReferenceException.
What do you think?

I hope you will enjoy the tips while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
11 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

Know More
6 MAR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
6 MAR
Angular 2 Master Class Fast Track Online Development Training (Online)

10:30 AM- 01:30 PM IST

Know More
27 FEB
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

Know More
25 FEB
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM-4:30 PM IST

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
14 FEB
.NET Development (Classroom)

11:00 AM-12:00 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

15 JAN
PPC Marketing (Classroom)

04:00 PM-05:30 PM

10 JAN
ASP.NET MVC with AngularJS Development (Online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

5 NOV
ASP.NET MVC with AngularJS Development (Classroom)

08:00 AM-09:30 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+