Tutorialspoint.dev

Is vs As operator keyword in C#

The difference between is and as operators are as follows:

  • The is operator is used to check if the run-time type of an object is compatible with the given type or not whereas as operator is used to perform conversion between compatible reference types or Nullable types.
  • The is operator is of boolean type whereas as operator is not of boolean type.
  • The is operator returns true if the given object is of the same type whereas as operator returns the object when they are compatible with the given type.
  • The is operator returns false if the given object is not of the same type whereas as operator return null if the conversion is not possible.
  • The is operator is used for only reference, boxing, and unboxing conversions whereas as operator is used only for nullable, reference and boxing conversions

Example of is operator:

// C# program to illustrate the
// use of is operator keyword
using System;
  
// taking a class
public class P { }
  
// taking a class
// derived from P
public class P1 : P { }
  
// taking a class
public class P2 { }
  
// Driver Class
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating an instance
        // of class P
        P o1 = new P();
  
        // creating an instance
        // of class P1
        P1 o2 = new P1();
  
        // checking whether 'o1'
        // is of type 'P'
        Console.WriteLine(o1 is P);
  
        // checking whether 'o1' is
        // of type Object class
        // (Base class for all classes)
        Console.WriteLine(o1 is Object);
  
        // checking whether 'o2'
        // is of type 'P1'
        Console.WriteLine(o2 is P1);
  
        // checking whether 'o2' is
        // of type Object class
        // (Base class for all classes)
        Console.WriteLine(o2 is Object);
  
        // checking whether 'o2'
        // is of type 'P'
        // it will return true as P1
        // is derived from P
        Console.WriteLine(o2 is P1);
  
        // checking whether o1
        // is of type P2
        // it will return false
        Console.WriteLine(o1 is P2);
  
        // checking whether o2
        // is of type P2
        // it will return false
        Console.WriteLine(o2 is P2);
    }
}

Output:

True
True
True
True
True
False
False

Example of as operator:

// C# program to illustrate the
// concept of 'as' operator
using System;
  
// Classes
class Y { }
class Z { }
  
class GFG {
  
    // Main method
    static void Main()
    {
  
        // creating and initializing object array
        object[] o = new object[5];
        o[0] = new Y();
        o[1] = new Z();
        o[2] = "Hello";
        o[3] = 4759.0;
        o[4] = null;
  
        for (int q = 0; q < o.Length; ++q) {
  
            // using as operator
            string str1 = o[q] as string;
  
            Console.Write("{0}:", q);
  
            // checking for the result
            if (str1 != null) {
                Console.WriteLine("'" + str1 + "'");
            }
            else {
                Console.WriteLine("Is is not a string");
            }
        }
    }
}

Output:

0:Is is not a string
1:Is is not a string
2:'Hello'
3:Is is not a string
4:Is is not a string


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter