Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to implement is functionality without using is keyword in C#

  • Last Updated : 01 May, 2019

Prerequisite: is Keyword and as Keyword

Before implementing is operation without is keyword, let us have a brief outlook on how is keyword actually works.

is keyword

In C# is keyword is used to evaluate compatibility at runtime. It determines whether an object instance or the result of an expression can be converted to a specified type.

Syntax:

expr is type

Example: In the below code class Program class inherits a class c1 and in “if condition” the compatibility is checked using is operator which returns true.




// C# program to illustrate the  
// use of 'is' operator keyword 
using System;
  
namespace GeeksforGeeks {
  
// Taking an empty class
class c1 {
  
  
// Program class inherits c1
class Program : c1 
{
  
    // Main Method
    static void Main(string[] args)
    {
  
        // object of Program class
        Program pro1 = new Program(); 
  
        // dynamic check of compatibility.
        // pro1 is the object of Program class
        // and c1 is class which is inherited by 
        // Program class
        if (pro1 is c1) 
        {
            Console.WriteLine("GeeksForGeeks");
        }
    }
}
}
Output:



GeeksForGeeks

Is implementation without using is keyword

The similar functionality can be implemented using as keyword which returns

  • null if the compatibility is not set
  • and in case of compatible operands, it returns the LHS operand.

Example: In the below code, the object of c1 class is pro2 and thus cannot be assigned as an instance of Program class thus obj2 is set as null which later on is checked in if condition.




// C# program to illustrates the is keyword 
// functionality without using is keyword
using System;
  
namespace GeeksforGeeks {
  
// Taking an empty class
class c1 { } 
  
// Class Program inherits the class c1
class Program : c1
{
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating the object of Program class
        Program pro1 = new Program();
  
        // Creating the object of c1 class
        c1 pro2 = new c1();
  
        // Here, assigns obj1 with pro1 i.e. 
        // LHS operand as compatibility is set
        var obj1 = pro1 as c1;
  
        // Here, assigns obj2 with null as 
        // compatibility is not set
        var obj2 = pro2 as Program;
  
        // from above, obj1 is not null
        if (obj1 != null
        {
            Console.WriteLine("GeeksForGeeks");
        }
  
        // from above, obj2 is null
        if (obj2 != null
        {
            Console.WriteLine("This Line will not be printed");
        }
    }
}
}
Output:
GeeksForGeeks

Note: You can also find some significant differences between the is keyword and as keyword by going through the article Is vs As operator keyword in C#




My Personal Notes arrow_drop_up
Recommended Articles
Page :