Is Operator Keyword In C#

In this article, we will discuss the Is operator in C# with its syntax and examples.

What is the Is Operator?

The is operator determines if an object's run-time type is comparable with the specified type. It returns true if the provided object is of the same type; otherwise, it returns false. It also returns False for null items. The 'is' keyword is a strong and versatile construct that is essential for type checking and casting.

Syntax:

It has the following syntax:

Example

expression is type

Here, the expression will be converted into a type instance. Type is the name of the type to which the expression's result will be transformed. If the expression is not null and the object created by evaluating the expression can be converted to the provided type, the operator is going to return true. If not, it will return false .

Example 1:

Let us take an example to illustrate the Is operator in C#.

Example

using System;

class Employee {

	// the data numbers
	public string name;
	public int id;

	//The methods of the other class
	public void details(string na, int rs)
	{
		name = na;
		id = rs;
	}
}

class Dep {

	// the data members
	public int depnum;
	public int num;

	//The methods of dep
	public void count(int a1, int i1)
	{
		depnum = a1;
		num = i1;
	}
}

public class IsOperator {

	// Main method
	static public void Main()
	{

		// object creation 
		Employee a1 = new Employee();

		a1.details("Likith", 51);

		Dep d= new Dep();

		d.count(20, 30);

		bool res;
        // Use the is operator to determine 
        //whether 'a' is of the Employee type.
		res = a1 is Employee;
		Console.WriteLine("Is a is Employee? : {0}", res);

	
		res = d is Employee;
		Console.WriteLine("Is d is a Employee? : {0}", res);

		// a1 value as null
		a1 = null;

		// the null object
		res = a1 is Employee;
		Console.WriteLine("Is a is Employee? : {0}", res);
	}
}

Output:

Output

Is a is Employee? : True
Is d is a Employee? : False
Is a is Employee? : False

Example 2:

Let us take another example to illustrate the Is operator in C#.

Example

using System;

// taking a class
public class Isop1{
	
}

// the derived class
public class Isop2 : Isop1 {
	
}

// the class Isop3
public class Isop3 {
	
}


// Driver Class
public class IsOperator{
	
	// Main Method
	public static void Main()
	{
		// creating the instance of the class Isop1
		Isop1 ob1 = new Isop1();
		
		// instance creation for ISop2
		Isop2 ob2 = new Isop2();
		
		// checking whether the object belongs to class Isop1
		Console.WriteLine(ob1 is Isop1);
		
		// // Checking if 'ob1' is an Object 
		//class (the fundamental class for all classes).
		Console.WriteLine(ob1 is Object);
		
		// checking whether the object ob2 is belongs to class Isop2
		Console.WriteLine(ob2 is Isop2);
		
		//Checking if 'ob2' is an Object 
		//class (the fundamental class for all classes).
		Console.WriteLine(ob2 is Object);
		
		//Checking if 'ob2' is of type 'Isop1' 
                       //will return true because Isop2 is derived    from Isop1.
		Console.WriteLine(ob2 is Isop1);
		
		//verifying whether object ob1 is of type Isop3, it is going to return false
		Console.WriteLine(ob1 is Isop3);
		
		// Evaluating if ob2 is of type Isop3 
		//will result in false.
		Console.WriteLine(ob2 is Isop3);
		
		
	}
}

Output:

Output

True
True
True
True
True
False
False

Advantages of is Operator keyword in C#

Several advantages of the Is operator in C# are as follows:

  • Type checking:

The keyword checks if an object is a specific type at runtime. It enables us to develop code that handles objects dynamically based on their type.

  • Type Casting:

If the check is successful, we can securely cast an object to a specified type when we use it in combination with an assignment.

  • Avoiding Invalid Cast Exceptions:

In order to avoid invalid cast exceptions, use the is keyword to validate the type of the object before casting.

Input Required

This code uses input(). Please provide values below: