In the development of the software, typecasting is an inescapable thing. In many cases, one needs to convert an object(Type) into another object(Type) and sometimes got InvalidCastException. So, to overcome such types of exception C# provides is operator.
The is operator is used to check if the run-time type of an object is compatible with the given type or not. It returns true if the given object is of the same type otherwise, return false. It also returns false for null objects.
Syntax:
expression is type
Here, the expression will be evaluated to an instance of some type. And type is the name of the type to that the result of the expression is to be converted. If the expression is not null and the object results from evaluating the expression can be converted to the specified type then is operator will return true otherwise it will return false.
Example 1: In the below code, we have three classes i.e. Author, Work and GFG. GFG is the driver class which contains the Main method. Class 'Author' and 'Work' have data members and method. In the Main method, objects of class Author and Work created and methods of these classes are called using the instance of the class. After that, a bool value bool result; is taken to store the return value of is operator. The line of code i.e. result = a is Author; is used to check whether a(object of class author) is of type Author. It will return true as a is the instance of Author class. But instance w is not of type Author, that's why it returns false. After that, we are assigning null to object a which will give result false as comparing to an instance of Author.
Csharp
// C# program to illustrate the
// use of 'is' operator keyword
using System;
class Author {
// data members
public string name;
public int rank;
// method of Author class
public void details(string n, int r)
{
name = n;
rank = r;
}
}
class Work {
// data members
public int articl_no;
public int improv_no;
// method of Work class
public void totalno(int a, int i)
{
articl_no = a;
improv_no = i;
}
}
// Driver Class
public class GFG {
// Main method
static public void Main()
{
// Creating objects of
// Author and Work class
Author a = new Author();
a.details("Ankita", 5);
Work w = new Work();
w.totalno(80, 50);
bool result;
// Check 'a' is of Author
// type or not
// Using is operator
result = a is Author;
Console.WriteLine("Is a is Author? : {0}", result);
// Check w is of Author type
// using is operator
result = w is Author;
Console.WriteLine("Is w is Author? : {0}", result);
// Take the value of a is null
a = null;
// Check null object
// Using is operator
result = a is Author;
Console.WriteLine("Is a is Author? : {0}", result);
}
}
Output:
Is a is Author? : True
Is w is Author? : False
Is a is Author? : False
Example 2: In the below program, we are checking whether the derived type is of the expression type on the left-hand side of the is operator. If is derived then it will return true otherwise it returns false.
Csharp
// C# program to illustrate the
// use of is operator keyword
using System;
// taking a class
public class G1 {
}
// taking a class
// derived from G1
public class G2 : G1 {
}
// taking a class
public class G3 {
}
// Driver Class
public class GFG {
// Main Method
public static void Main()
{
// creating an instance
// of class G1
G1 obj1 = new G1();
// creating an instance
// of class G2
G2 obj2 = new G2();
// checking whether 'obj1'
// is of type 'G1'
Console.WriteLine(obj1 is G1);
// checking whether 'obj1' is
// of type Object class
// (Base class for all classes)
Console.WriteLine(obj1 is Object);
// checking whether 'obj2'
// is of type 'G2'
Console.WriteLine(obj2 is G2);
// checking whether 'obj2' is
// of type Object class
// (Base class for all classes)
Console.WriteLine(obj2 is Object);
// checking whether 'obj2'
// is of type 'G1'
// it will return true as G2
// is derived from G1
Console.WriteLine(obj2 is G1);
// checking whether obj1
// is of type G3
// it will return false
Console.WriteLine(obj1 is G3);
// checking whether obj2
// is of type G3
// it will return false
Console.WriteLine(obj2 is G3);
}
}
Output:
True
True
True
True
True
False
False
Note:
- Only reference, boxing, and unboxing conversions are considered by the is operator keyword.
- User-defined conversions or the conversion which are defined using the implicit and explicit are not considered consider by is operator. For the conversions which are known at the compile-time or handled by an implicit operator, is operator will give warnings for that.
Similar Reads
C# Operators In C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C++ | Nested Ternary Operator Ternary operator also known as conditional operator uses three operands to perform operation. Syntax : op1 ? op2 : op3; Nested Ternary operator: Ternary operator can be nested. A nested ternary operator can have many forms like : a ? b : ca ? b: c ? d : e ? f : g ? h : ia ? b ? c : d : e Let us unde
5 min read
C# | Operator Overloading Prerequisite: Operators in C# The concept of overloading a function can also be applied to operators. Operator overloading gives the ability to use the same operator to do various operations. It provides additional capabilities to C# operators when they are applied to user-defined data types. It ena
4 min read
C# Program to Check a Specified Type is a Pointer or not A pointer is a variable that contains the references of another variable. Or in other words, the pointer is a variable that stores the address of the same type of variable. For example, a string pointer can store the address of a string. In C#, we can able to check the given type is a pointer or not
2 min read
Relational Operators in C In C, relational operators are the symbols that are used for comparison between two values to understand the type of relationship a pair of numbers shares. The result that we get after the relational operation is a boolean value, that tells whether the comparison is true or false. Relational operato
4 min read
Logical OR operator in Programming In programming, Logical operators play a crucial role in manipulating individual bits of data. One of the fundamental logical operators is the Logical OR operator(||).In this article, weâll dive deep into what is a Logical OR operator, its syntax, properties, applications, and optimization technique
5 min read