Advertisement



< Prev
Next >



C# is Operator





In this tutorial, we are going to discuss an important operator provided by C# language, which is known as the is operator. Similar to Java, the C# language gives us a powerful feature of runtime type identification, which allows us to determine the type of an object by using the is operator during the runtime of a program.




Syntax of the is operator


expression is type





The is operator example


In the upcoming example, we are going to create two classes A and B, where the class A has inherited from the class B. Next. we are going to make their objects and use the is operator to determine the type of these objects.

//C# is Operator Example

using System;


class A
{
}

//Class B inheriting from class A
class B : A
{
public static void Main()
{
	//Creating an object of class A
	A a = new A();


	//Creating an object of class B
	B b = new B();
	
	
	//The first expression involving the is operator
	bool result = a is A;
	Console.WriteLine("Is a an object of type A : " + result);


	//The second expression involving the is operator
	result = a is B;
	Console.WriteLine("Is a an object of type B : " + result);

	
	//The third xpression involving the is operator
	result = b is A;
	Console.WriteLine("Is b an object of type A : " + result);

	
	//The fourth expression involving the is operator
	result = b is A;
	Console.WriteLine("Is b an object of type B : " + result);
}
}
Output
Is a an object of type A : True
Is a an object of type B : False
Is b an object of type A : True
Is b an object of type B : True


Program Analysis







  • The is operator always returns true if any object is tested against Object class.

  • //C# The is Operator Example, testing against the Object class i.e. mother of all class in C#
    
    using System;
    
    
    class A
    {
    }
    
    //Class B inheriting from class A
    class B : A
    {
    public static void Main()
    {
    	//Creating an object of class A
    	A a = new A();
    
    
    	//Creating an object of class B
    	B b = new B();
    	
    	
    	//The first expression involving the is operator
    	bool result = a is Object;
    	Console.WriteLine("Is a an object of type Object : " + result);
    
    
    	//The second expression involving the is operator
    	result = b is Object;
    	Console.WriteLine("Is b an object of type Object : " + result);
    }
    }


    Output:


    Is a an object of type Object : True
    Is b an object of type Object : True

    In the last code, we have created two objects, each of class A and B and then we have checked their type against the Object class i.e. mother of all classes in C#, which, as expected turns out to be true.


    Advertisement






  • The is operator can only be checked against the concrete initialized object.

  • In the upcoming example, we are going to show you how using the is operator with an uninitialized object returns a compile error.

    //C# The is operator testing against uninitialized objects
    
    
    using System;
    
    class A
    {
    public static void Main()
    {
    	//Creating a reference variable object of class A i.e uninitialized object
    	A a;
    
    
    	//The first expression involving the is operator
    	bool result = a is A;
    	Console.WriteLine("Is a an object of type A : " + result);
    }
    }


    Compile Error:


    A.cs(14,16): error CS0165: Use of unassigned local variable 'a'





  • The is operator checked on null will always returns false.

  • In the upcoming example, we are going to show you how using the is operator with null always returns false.

    //C# The is oprator example with null
    
    using System;
    
    class A
    {
    public static void Main()
    {	
    	//The second expression involving the is operator
    	bool result = null is A;
    	Console.WriteLine("Is null an object of type A : " + result);
    
    
    	//The third expression involving the is operator
    	result = null is Object;
    	Console.WriteLine("Is null an object of type Object : " + result);
    }
    }


    Output:


    Is null an object of type A : False
    Is null an object of type Object : False

    The null is an uninitialized value and therefore checking it against any class type will return the bool false value.



    Please share this article -





    < Prev
    Next >
    < Logical Operators in C++
    Scope Resolution Operator >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement