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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
        }
    }
}
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
        }
    }
}
}

chevron_right


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

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

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 :

1


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