C# – The ‘is’ and ‘as’ operators

The as operator tries to convert an object to the target type:

Person person = new Employee() { Name = "Julius Caesar", Position = "Senior .NET Dev" }; var employee = person as Employee;
Code language: C# (cs)

The is operator returns true if the object can be converted to the target type:

Person person = new Employee() { Name = "Julius Caesar", Position = "Senior .NET Dev" }; if (person is Employee) { Console.WriteLine($"We're dealing with an employee here"); }
Code language: C# (cs)

You can also use the is operator to declare a variable of the target type (this is referred to as the declaration pattern). This is a nice shortcut.

Person person = new Employee() { Name = "Julius Caesar", Position = "Senior .NET Dev" }; if (person is Employee employee) { Console.WriteLine($"We're dealing with {employee.Name} - {employee.Position} here"); }
Code language: C# (cs)

Note: The employee object is only available in the if block. Intellisense confusingly shows this variable as in scope out of the if block, but if you try to use it, you’ll see a “Use of unassigned variable” compiler error.

In the rest of this article, I’ll go into further details about the ‘as’ and ‘is’ operators in a few scenarios.

What happens when the object can’t be converted to the target type?

When you try to explicitly cast an object to a type it can’t be converted to, it’ll throw System.InvalidCastException. For example:

object animal = new Dog() { Name = "Caesar" }; var person = (Person)animal;
Code language: C# (cs)

How do the as and is operators compare?

as operator

When the as operator can’t convert to a target type, it’ll return a null. Here’s an example:

object animal = new Dog() { Name = "Caesar" }; var person = animal as Person; if (person is null) { Console.WriteLine($"Person is null"); }
Code language: C# (cs)

This outputs:

Person is null
Code language: plaintext (plaintext)

Since the as operator returns nulls when the conversion can’t be done, you’ll need to do null checks or use the null-conditional (?.) operator when using the object.

is operator

The is operator will return false if the object can’t be converted to the target type. Here’s an example:

object animal = new Dog() { Name = "Caesar" }; if (animal is Person) { Console.WriteLine("Animal is a person"); } else { Console.WriteLine("This animal isn't a person!"); }
Code language: C# (cs)

This outputs:

This animal isn't a person!
Code language: plaintext (plaintext)

What happens if the object is null?

as operator

When you try to use the as operator on a null object, it will return a null. Here’s an example:

Person person = null; var employee = person as Employee; if (employee is null) { Console.WriteLine("Employee is null"); }
Code language: JavaScript (javascript)

Note: This is the same behavior as explicitly casting.

This outputs:

Employee is null
Code language: plaintext (plaintext)

is operator

When the object is null, the is operator will return false. Here’s an example:

Employee employee = null; if (employee is Employee) { Console.WriteLine("It's an employee"); } else { Console.WriteLine("It's not an employee, or it was null"); }
Code language: JavaScript (javascript)

This outputs:

It's not an employee, or it was null
Code language: plaintext (plaintext)

Checking more conditions on the is operator’s declared variable

In addition to using the is operator’s declared variable within the if block, you can also use it in the conditional statement itself.

The following example is using the declared variable (employee) to check if the employee’s name is “Julius Caesar.”

Person person = new Employee() { Name = "Julius Caesar", Position = "Senior .NET Dev" }; if (person is Employee employee && employee.Name == "Julius Caesar") { Console.WriteLine($"All hail {employee.Position} Caesar!"); }
Code language: C# (cs)

This outputs:

All hail Senior .NET Dev Caesar!
Code language: plaintext (plaintext)

Without this, you’d need to check the extra condition in a nested if statement.

What if the object can’t be converted to the target type? It may seem like it would unsafe to check a condition on the declared variable (i.e. checking if the employee’s name is Julius Caesar), but that’s not the case. C# uses short-circuit evaluation. The is operator will return false, and the other conditions in the if statement won’t be checked.

Leave a Comment