IS vs AS operators : Performance Implications

If you are working as a C# programmer. You must know, the differences and performance implications of these operators IS vs AS. Some people who has not enough information about this, it’ll be helpful to them.

There are two operators IS as AS in C# and mostly used for same purposes. During our programming, we generally typecast the object into other type.

There are various scenarios, if you are typecasting , you may compile time error and other Run time error. Which obviously is not good for any application.

One thing, first I want to discuss, that as we know, System.Object is mother of all classes in C#. It means, whether you derive your class from System.Object specifically or not, CLR does this for you. So every object has always few basic method, that are inherited from System.Object. GetType also comes from System.Object. This always gives you the exact type of your object at run time. This method is non virtual so yo are not allowed to override this.

Now lets move to Is operator. For the safety of the code, whenever you are going to cast an object to other, say Class Student . you will be writing the code like

if( myobject is Student )
 { Student objStudent = (Student) myobject;
}

Obviously this is good code, because before typecasting to Student type, it is actually checking whether this is of Student type or not. Is Operator returns true if the object is of the same type else return false.

But what happens, if we write the code simply

 Student objStudent = (Student) myobject;

and where myobject is not of Student type, it’ll throw an exception. Actually at run time, CLR checks whether the object is of student type or not and it is not, so it throws an exception.

Now lets move to AS operator, this operator try to to typecast the object to a given type and if the typecasting is successful then it returns it as of object of that class else returns null.

Now we can write the above code as following,

 Student objStudent =  myobject as Student;
 if(objStudent !=null)
 {
 //use the object
 }

Here as we see in the first line, CLR first checks whether the object is of Student type then return object of that type else null. Actually type checking is not a simple task for CLR, it goes through all the hierarchy, i e base type and checks whether the object is of the current type or any base type.

So in the second one, CLR is checking this once while earlier one it is doing two time, first with IS operator and later in typecasting, which is obviously a overhead.

Here I also want to mention one thing, we should use AS operator judiciously, because it will never throw an exception while typecasting, so one always must have a check for null, before using this else it’ll through an exception.

On the other hand, if you are assign any value , say a input box or Label, one should use as operator. We can do in two ways, say we got one data row that came from database. Now,

textbox1.Text = data row["columnName"] as String;

because Text property can be assign any string or null, it will never barf. So this will work every time, but if you write

textbox1.Text = data row["columnName"].ToString();

It will be working fine till the value is not null else will through an exception because ToString() cannot be called over null value.

Hope you all have enjoyed this.

Happy coding!!

Brij