Skip to main content

How to Check an Object's Type with the is Pattern in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A value typed as object could be a number, a piece of text or anything else. In C#, item is int number checks whether it's an int and, when it is, gives it to you as an int called number in the same step, without a separate cast.

Syntax​

Syntax
if (<value> is <Type> <name>)
{
<code that uses name as a Type>
}
PartWhat it does
<value>The value to test, often of type object or of a base type.
is <Type>true when the value isn't null and its run-time type is <Type> or derives from it.
<name>A new variable holding the value as a <Type>. It's only assigned when the test is true.

Microsoft's patterns reference: "Use declaration and type patterns to check if the run-time type of an expression is compatible with a given type. By using a declaration pattern, you can also declare a new local variable."

The Example​

An object array holds three different kinds of value: the number 42, the text Leo and the decimal number 3.5. The example looks at each one and handles it according to its type.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which loops over an object array of 42, Leo and 3.5, prints number plus 1 when the item is an int, the text in capitals when it's a string, and Something else otherwise.

main.cs
object[] items = [42, "Leo", 3.5];

foreach (object item in items)
{
if (item is int number)
{
Console.WriteLine($"An int: {number + 1}");
}
else if (item is string text)
{
Console.WriteLine($"A string: {text.ToUpper()}");
}
else
{
Console.WriteLine($"Something else: {item}");
}
}

Inside each branch, the new variable has the tested type: number + 1 is arithmetic on an int, and text.ToUpper() is a string method. Neither would compile on item, which is an object.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
An int: 43
A string: LEO
Something else: 3.5

A zsh terminal where dotnet run main.cs prints An int: 43, A string: LEO and Something else: 3.5.

42 matched int, Leo matched string, and 3.5, a double, matched neither, so it went to the else. The command is the same in zsh, bash and PowerShell.

The Variable Only Exists Where the Test Passed​

The compiler lets you use the new variable only where the test is certain to have been true:

main.cs
object item = "Leo";
if (item is int number)
{
Console.WriteLine(number);
}

Console.WriteLine(number);
Output
/Users/you/demo/main.cs(7,19): error CS0165: Use of unassigned local variable 'number'

The build failed. Fix the build errors and run again.

After the if, item may not have been an int, so number may hold nothing. Use the variable inside the if block.

Common Mistakes​

MistakeWhat happensDo this instead
Using the pattern variable after the iferror CS0165: Use of unassigned local variable 'number'Use it inside the block where the test was true
Casting first, as in (int)item, and testing afterwardsAn InvalidCastException when the value isn't an intTest and convert in one step with is int number
Testing double values with is int3.5 is a double, so it never matches intTest for the type the value really has

Troubleshooting​

SymptomLikely causeFix
CS0165 for a pattern variableIt's used where the match isn't guaranteedMove the code into the if block
A value never matches although it looks rightIt's null, or its type is a different one, such as double instead of intPrint item?.GetType().Name to see the real type

FAQ​

Does null match a type pattern?​

No. The reference: "A declaration pattern with type T matches an expression when an expression result is non-null" and its type fits. So is string text is false for a null value, and text is never null inside the block.

Does is int also match types derived from it?​

For classes, yes: a declaration pattern also matches when the run-time type derives from the tested type or implements it as an interface. int is a struct, so nothing derives from it.

Conclusion​

if (item is int number) tests the type and gives you a typed variable in one step. It never matches null, and the variable can only be used where the test passed.

Additional Resources​