Home > C# > C# is and as operators

C# is and as operators

The below excerpt is from the book: Applied Microsoft .Net Framework Programming by Jeffrey Richter. If you are a C# programmer and you are not aware of is and as operators in C#, then reading the following extract would be very useful.

C# offers another way to cast using the is operator. The is operator checks whether an object is compatible with a given type, and the result of the evaluation is a Boolean: true or false. The is operator will never throw an exception. The following code demonstrates:

System.Object o = new System.Object();

System.Boolean b1 = (o is System.Object); // b1 is true.

System.Boolean b2 = (o is Employee); // b2 is false.

 

If the object reference is null, the is operator always returns false because there is no object available to check its type. The is operator is typically used as follows:

if (o is Employee) {

Employee e = (Employee) o;

// Use e within the ‘if’ statement.

}

 

In this code, the CLR is actually checking the object’s type twice: the is operator first checks to see if o is compatible with the Employee type. If it is, then inside the if statement, the CLR again verifies that o refers to an Employee when performing the cast. Because this programming paradigm is quite common, C# offers a way to simplify this code and improve its performance by providing an as operator:

Employee e = o as Employee;

if (e != null) {

// Use e within the ‘if’ statement.

}

 

In this code, the CLR checks if o is compatible with the Employee type, and if it is, as returns a non−null pointer to the same object. If o is not compatible with the Employee type, then the as operator returns null. Notice that the as operator causes the CLR to verify an object’s type just once. The if statement simply checks whether or not e is null—this check can be performed much more efficiently than verifying an object’s type.

The as operator works just like casting except the as operator will never throw an exception. Instead, if the object can’t be cast, the result is null. You’ll want to check to see whether the resulting reference is null, or attempting to use the resulting reference will cause a System.NullReferenceException exception to be thrown. The following code demonstrates:

System.Object o = new System.Object(); // Creates a new Object object

Employee e = o as Employee; // Casts o to an Employee

// The cast above fails: no exception is thrown, but e is set to null.

e.ToString(); // Accessing e throws a NullReferenceException.

About these ads
Categories: C#
  1. hwizard
    Sunday, January 6, 2008 at 7:24 pm

    If Employee is derived from Person,
    will the following will be valid:
    Person p = e as Person;

    • Adarsh Chaurasia
      Wednesday, August 21, 2013 at 3:53 pm

      In this case, you dont need to do this. Because you can assign derived class object to the base class variable.

  2. Balaji
    Thursday, June 12, 2008 at 9:47 am

    Very usefull

    Thanks

  3. Mahesh
    Wednesday, October 15, 2008 at 11:58 pm

    Thanks Krishna,It was asked in the interview.

  4. Tuesday, October 28, 2008 at 3:03 am

    Great work, really helpful.
    is: OBJECT compatible with TYPE. (T/F)
    as: works just like CASTING and never throw exception. (Null/non-null reference.)

  5. PANKAJ
    Thursday, January 22, 2009 at 2:10 pm

    Very useful, Thanks a lot Krishna. Keep it up.

  6. Wednesday, June 17, 2009 at 7:48 pm

    Excellent

  7. upendra
    Wednesday, July 15, 2009 at 3:48 pm

    very easy to understand

  8. Vish
    Monday, November 30, 2009 at 12:06 am

    Very help full…

  9. Mahesh
    Tuesday, December 8, 2009 at 3:27 pm

    Very useful and easy to understand..

  10. Wednesday, March 3, 2010 at 10:48 am

    a as b ;
    Does it mean a is of type b?
    Thank u

  11. samy
    Sunday, May 16, 2010 at 9:46 pm

    hi Krish,

    First of all thanks….

    In which version, these operators were introduced?

    Thanks.

  12. sibadata nayak
    Monday, July 12, 2010 at 10:38 am

    What Is “as” Operator in C#.net and what is its Use ?

  13. sunayana
    Thursday, August 19, 2010 at 8:17 am

    this article is really very very gud. thanks

  14. Andrew Muliar
    Wednesday, November 24, 2010 at 8:50 pm

    Thank you for this explanation

  15. Thursday, June 30, 2011 at 2:48 pm

    Useful! Thanks.

  16. Rajish PV
    Monday, October 3, 2011 at 5:58 pm

    Very Useful, Thanks

  17. Hema
    Monday, March 19, 2012 at 4:19 pm

    Thanks it was very much useful :) keep posting :)

  18. rahul
    Wednesday, July 25, 2012 at 11:18 pm

    “If the object reference is null, the is operator always returns false because there is no object available to check its type” – I verified that (null is Object) returns True. Unless I am missing it completely, if i think about it, it makes sense to me as null is a valid value for any reference type.

  1. Monday, February 15, 2010 at 6:14 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: