Is operator in C#


Is operator also called as the type-compatibility operator plays an integral role in C# structures. Let’s try to understand this operator.

C#’s Is operator checks if the given object is compatible with another object and gives the result as true, if it is compatible. Else returns false.

Syntax

expression is obj

Example

Expression is the object which you like to check compatibility with. The expression can include variables, literals and method calls. Obj is the type against which the expression is verified. This can comprise of built-in and user-defined types.

// The operation of the type compatibility operator is performed.
Console.Writeline("Happy Holidays" is string);
Console.Writeline(42 is string);

Output

True
False

Let's understand this output. We know that “Happy Holidays” is a string literal and 42 is an integer. When “Happy Holidays” is checked against string data type, the result is true as it is compatible. The 42 when checked against string yields false as it is not compatible.

Expressions

Literal Expressions

The literal expressions consist of numbers, sequence of characters (string), arrays, etc.

Example

// The operation of the type compatibility operator is performed.
Console.Writeline("Happy Holidays" is string);

Output

TRUE

Variable Expressions

The variable expressions will contain the objects which act as containers holding values or references.

Example

// an object is declared with string data type.
object str= "Happy Holidays";
// The operation of the type compatibility operator is performed.
Console.Writeline(str is string);

Output

TRUE

Function call Expressions

The function call expressions will have function calls in the left-hand side of the is operator.

Example

// A class declaration
class class_dec{}
// an object is declared.
object str= Method_in_the_class();
// The operation of the type compatibility operator is performed.
Console.Writeline(str is class_dec);

Output

TRUE

In the above example, the function call statement is checked for type compatibility. As long as the function being called is declared in the type. The result would be true. In this case, the result would turn out to be false. The class_dec is an empty class.

Types

Built-in Types

The predefined types in the C# can be used in the right hand side of the is operator. It can be integer, char, float and boolean.

Example

// an object is declared with numeric data type.
object num= 42;
// The operation of the type compatibility operator is performed.
Console.Writeline(num is int);

Output

TRUE

User-defined Types

The types defined by the user can also be checked by the is operator. It consists of classes, enumerations, etc.

Example

// A class declaration
class class_dec{}
// an instance of the class is declared.
class_dec str= new class_dec();
// The operation of the type compatibility operator is performed.
Console.Writeline(str is class_dec);

Output

TRUE

In the above example, the is operator compares the object against the user defined datatype.

Note − The is operator can also be used with NULL. If the expression is not null, then the operator will always yield false as the output.

The scope of the user-defined type can affect the output. The is operator should always be used in the scope of the type declared.

Conclusion

In this article, we focussed on the is operator in C#. We analyzed the syntax and understood the various instances in which the is operator can be used. The usage of is operator is illustrated using various code snippets and examples.

Updated on: 25-Apr-2023

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements