C# | is Operator Keyword

In the development of the software, typecasting is an inescapable thing. In many cases, one needs to convert an object(Type) into another object(Type) and sometimes got InvalidCastException. So, to overcome such types of exception C# provides is operator.
The is operator is used to check if the run-time type of an object is compatible with the given type or not. It returns true if the given object is of the same type otherwise, return false. It also returns false for null objects.

Syntax:

expression is type

Here, the expression will be evaluated to an instance of some type. And type is the name of the type to that the result of the expression is to be converted. If the expression is not null and the object results from evaluating the expression can be converted to the specified type then is operator will return true otherwise it will return false.

Example 1: In the below code, we have three classes i.e. Author, Work and GFG. GFG is the driver class which contains the Main method. Class ‘Author’ and ‘Work’ have data members and method. In the Main method, objects of class Author and Work created and methods of these classes are called using the instance of the class. After that, a bool value bool result; is taken to store the return value of is operator. The line of code i.e. result = a is Author; is used to check whether a(object of class author) is of type Author. It will return true as a is the instance of Author class. But instance w is not of type Author, that’s why it returns false. After that, we are assigning null to object a which will give result false as comparing to an instance of Author.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// use of 'is' operator keyword
using System;
  
class Author {
  
    // data members
    public string name;
    public int rank;
  
    // method of Author class
    public void details(string n, int r)
    {
        name = n;
        rank = r;
    }
}
  
class Work {
  
    // data members
    public int articl_no;
    public int improv_no;
  
    // method of Work class
    public void totalno(int a, int i)
    {
        articl_no = a;
        improv_no = i;
    }
}
  
// Driver Class
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating objects of 
        // Author and Work class
        Author a = new Author();
  
        a.details("Ankita", 5);
  
        Work w = new Work();
  
        w.totalno(80, 50);
  
        bool result;
  
        // Check 'a' is of Author 
        // type or not
        // Using is operator
        result = a is Author;
        Console.WriteLine("Is a is Author? : {0}", result);
  
        // Check w is of Author type
        // using is operator
        result = w is Author;
        Console.WriteLine("Is w is Author? : {0}", result);
  
        // Take the value of a is null
        a = null;
  
        // Check null object
        // Using is operator
        result = a is Author;
        Console.WriteLine("Is a is Author? : {0}", result);
    }
}

chevron_right


Output:



Is a is Author? : True
Is w is Author? : False
Is a is Author? : False

Example 2: In the below program, we are checking whether the derived type is of the expression type on the left-hand side of the is operator. If is derived then it will return true otherwise it returns false.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

True
True
True
True
True
False
False

Note:

  • Only reference, boxing, and unboxing conversions are considered by the is operator keyword.
  • User-defined conversions or the conversion which are defined using the implicit and explicit are not considered consider by is operator. For the conversions which are known at the compile-time or handled by an implicit operator, is operator will give warnings for that.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.