In this guide, we will explore the Is operator in C# along with its syntax and sample illustrations.
What is the Is Operator?
The is operator checks whether the runtime type of an object is compatible with a specific type. It yields a true value when the object matches the specified type and false when it doesn't. This operator also evaluates to False when handling null objects. 'is' is a powerful and flexible keyword crucial for type validation and conversion.
Syntax:
It has the following syntax:
expression is type
Here, the expression will be transformed into an instance of a specific type. The type represents the category to which the result of the expression will be changed. If the expression is not empty and the object generated by assessing the expression can be changed to the specified type, the operator will yield true. Otherwise, it will produce false.
Example 1:
Let's consider a scenario to demonstrate the Is operator in C#.
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:
Is a is Employee? : True
Is d is a Employee? : False
Is a is Employee? : False
Example 2:
Let's consider another instance to demonstrate the Is operator in C#.
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:
True
True
True
True
True
False
False
Advantages of is Operator keyword in C#
One of the key benefits of using the Is operator in C# is its capability for type validation.
The keyword verifies whether an object belongs to a particular type during program execution. This functionality allows us to create flexible code that can adapt to different object types dynamically.
- Type Conversion:
If the verification process is successful, we can safely convert an object to a designated data type by employing it alongside an assignment operation.
- Preventing Invalid Type Conversions:
To prevent invalid cast exceptions, employ the is keyword to verify the object's type prior to performing a cast operation.