Deepak  Kamboj
What is difference between is and as operators in C#?
By Deepak Kamboj in C# on Jun 13 2013
  • Jignesh Kumar
    Aug, 2018 26

    As operator and is operator both are used for typecasting check. Is operator used for check type casting of object it will return Boolean value true if typecasting is success and it will throw exception in case it fails to type cast. As operator also does the same thing but it is safe way of doing type cast because it will return null in case it fails to type cast.

    • 1
  • Ajay Gandhi
    Aug, 2015 24

    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 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 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. AS operator performs only reference conversions, nullable conversions, and boxing conversions. This operator cannot perform other conversions like as user-defined conversions.

    • 0
  • Bir Singh
    Jul, 2015 2

    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 As operator is similar to a cast, but returns null instead of an exception if it fails

    • 0
  • Pramod Verma
    Jan, 2015 24

    Is operator return true and false. As operator return object.

    • 0
  • Mohit Kumar
    Apr, 2014 12

    IS Is key word is useful for Checking if an object is compatible with a given type or not.As Is Helpful in Type casting by using As Key word you can do up casting and down casting operations. For improving your skills you can refer this link. c# interview questions

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS