Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Is operator in C#
The is operator in C# is a type-compatibility operator that checks if an object is compatible with a specific type. It returns true if the object is compatible with the specified type, otherwise it returns false. This operator is essential for type checking and safe casting operations in C#.
Syntax
Following is the basic syntax of the is operator
expression is type
Where expression is the object to check, and type is the type to check against.
Basic Type Checking
Example
using System;
class Program {
public static void Main() {
Console.WriteLine("Happy Holidays" is string);
Console.WriteLine(42 is string);
Console.WriteLine(42 is int);
Console.WriteLine(3.14 is double);
}
}
The output of the above code is
True False True True
Using 'is' with Variables
Example
using System;
class Program {
public static void Main() {
object str = "Happy Holidays";
object num = 42;
object arr = new int[] {1, 2, 3};
Console.WriteLine(str is string);
Console.WriteLine(num is int);
Console.WriteLine(arr is int[]);
Console.WriteLine(num is string);
}
}
The output of the above code is
True True True False
Using 'is' with User-Defined Types
Example
using System;
class Vehicle {
public string Name { get; set; }
}
class Car : Vehicle {
public int Doors { get; set; }
}
class Program {
public static void Main() {
Car myCar = new Car();
Vehicle myVehicle = new Car();
object obj = new Car();
Console.WriteLine(myCar is Car);
Console.WriteLine(myCar is Vehicle);
Console.WriteLine(myVehicle is Car);
Console.WriteLine(obj is Car);
Console.WriteLine(obj is Vehicle);
}
}
The output of the above code is
True True True True True
Using 'is' with null Values
Example
using System;
class Program {
public static void Main() {
object nullObject = null;
string nullString = null;
Console.WriteLine(nullObject is string);
Console.WriteLine(nullObject is object);
Console.WriteLine(nullString is string);
Console.WriteLine(nullString is object);
}
}
The output of the above code is
False False False False
Pattern Matching with 'is' (C# 7.0+)
Since C# 7.0, the is operator supports pattern matching, allowing you to check type and extract the value in a single operation
Example
using System;
class Program {
public static void Main() {
object[] items = { "Hello", 42, 3.14, true };
foreach (object item in items) {
if (item is string str) {
Console.WriteLine($"String: {str}");
}
else if (item is int number) {
Console.WriteLine($"Integer: {number}");
}
else if (item is double d) {
Console.WriteLine($"Double: {d}");
}
else {
Console.WriteLine($"Other type: {item}");
}
}
}
}
The output of the above code is
String: Hello Integer: 42 Double: 3.14 Other type: True
Comparison with typeof and GetType()
| Method | Purpose | Inheritance |
|---|---|---|
is operator |
Type compatibility check | Returns true for base types |
GetType() |
Exact type information | Returns exact type only |
typeof() |
Type metadata | Gets Type object at compile time |
Conclusion
The is operator in C# provides a safe way to check type compatibility, supporting inheritance hierarchies and null checking. With pattern matching support in modern C#, it becomes even more powerful for type checking and value extraction in a single operation.
