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
What is the use of "is" keyword in C#?
The is keyword in C# is used to check if an object is of a specific type or can be cast to that type. It returns a Boolean value ? true if the object is compatible with the specified type, false otherwise.
The is keyword is particularly useful for type checking before performing type conversions, ensuring safe downcasting in inheritance hierarchies, and working with polymorphic objects.
Syntax
Following is the syntax for using the is keyword −
object is Type
The is keyword can also be used with pattern matching (C# 7.0+) −
if (object is Type variable) {
// use 'variable' directly
}
Using 'is' for Type Checking
The is keyword checks type compatibility in inheritance hierarchies. It returns true for the actual type, base types, and implemented interfaces −
Example
using System;
class Employee {
public int ID { get; set; }
public string Name { get; set; }
}
class PermanentEmployee : Employee {
public int AnnualSalary { get; set; }
}
class ContractEmployee : Employee {
public int HourlySalary { get; set; }
}
class Program {
static void Main() {
Employee emp = new PermanentEmployee {
ID = 1,
Name = "Martin"
};
// Returns true as the derived type can be converted to base type
if (emp is Employee) {
Console.WriteLine(emp.Name + " is Employee");
}
else {
Console.WriteLine(emp.Name + " is not Employee");
}
// Returns true, as the actual object is of type PermanentEmployee
if (emp is PermanentEmployee) {
Console.WriteLine(emp.Name + " is PermanentEmployee");
}
else {
Console.WriteLine(emp.Name + " is not PermanentEmployee");
}
// Returns false, as PermanentEmployee object cannot be converted to ContractEmployee
if (emp is ContractEmployee) {
Console.WriteLine(emp.Name + " is ContractEmployee");
}
else {
Console.WriteLine(emp.Name + " is not ContractEmployee");
}
}
}
The output of the above code is −
Martin is Employee Martin is PermanentEmployee Martin is not ContractEmployee
Using 'is' with Pattern Matching
C# 7.0 introduced pattern matching with the is keyword, allowing you to check the type and assign it to a variable in one operation −
Example
using System;
class Animal {
public string Name { get; set; }
}
class Dog : Animal {
public void Bark() {
Console.WriteLine($"{Name} is barking!");
}
}
class Cat : Animal {
public void Meow() {
Console.WriteLine($"{Name} is meowing!");
}
}
class Program {
static void Main() {
Animal[] animals = {
new Dog { Name = "Buddy" },
new Cat { Name = "Whiskers" },
new Animal { Name = "Generic" }
};
foreach (Animal animal in animals) {
if (animal is Dog dog) {
dog.Bark();
}
else if (animal is Cat cat) {
cat.Meow();
}
else {
Console.WriteLine($"{animal.Name} is just an animal");
}
}
}
}
The output of the above code is −
Buddy is barking! Whiskers is meowing! Generic is just an animal
Using 'is' with Built-in Types
Example
using System;
class Program {
static void ProcessValue(object value) {
if (value is int intValue) {
Console.WriteLine($"Integer: {intValue * 2}");
}
else if (value is string stringValue) {
Console.WriteLine($"String: {stringValue.ToUpper()}");
}
else if (value is bool boolValue) {
Console.WriteLine($"Boolean: {!boolValue}");
}
else {
Console.WriteLine($"Unknown type: {value?.GetType().Name}");
}
}
static void Main() {
ProcessValue(42);
ProcessValue("hello");
ProcessValue(true);
ProcessValue(3.14);
}
}
The output of the above code is −
Integer: 84 String: HELLO Boolean: False Unknown type: Double
Conclusion
The is keyword in C# provides safe type checking by returning a Boolean value indicating whether an object is of a specific type. With pattern matching support in C# 7.0+, it also allows immediate variable assignment when the type check succeeds, making code more concise and readable.
