The is operator checks if the result of an expression is compatible with a given type. For information about the type-testing is operator, see the is operator section of the Type-testing and cast operators article. You can also use the is operator to match an expression against a pattern, as the following example shows:
sample · csharpdocs/csharp/language-reference/operators/snippets/shared/IsOperator.csIntroExample
namespace operators;
public static class IsOperator
{
public static void Examples()
{
DeclarationPattern();
IsFirstFridayOfOctober(DateTime.Now);
NullCheck(default(object));
NonNullCheck(new object());
ListPattern();
}
// <IntroExample>
static bool IsFirstFridayOfOctober(DateTime date) =>
date is { Month: 10, Day: <=7, DayOfWeek: DayOfWeek.Friday };
// </IntroExample>
private static void DeclarationPattern()
{
// <DeclarationPattern>
int i = 34;
object iBoxed = i;
int? jNullable = 42;
if (iBoxed is int a && jNullable is int b)
{
Console.WriteLine(a + b); // output 76
}
// </DeclarationPattern>
}
private static void NullCheck(object input)
{
// <NullCheck>
if (input is null)
{
return;
}
// </NullCheck>
}
private static void NonNullCheck(object result)
{
// <NonNullCheck>
if (result is not null)
{
Console.WriteLine(result.ToString());
}
// </NonNullCheck>
}
private static void ListPattern()
{
// <ListPatterns>
int[] empty = [];
int[] one = [1];
int[] odd = [1, 3, 5];
int[] even = [2, 4, 6];
int[] fib = [1, 1, 2, 3, 5];
Console.WriteLine(odd is [1, _, 2, ..]); // false
Console.WriteLine(fib is [1, _, 2, ..]); // true
Console.WriteLine(fib is [_, 1, 2, 3, ..]); // true
Console.WriteLine(fib is [.., 1, 2, 3, _ ]); // true
Console.WriteLine(even is [2, _, 6]); // true
Console.WriteLine(even is [2, .., 6]); // true
Console.WriteLine(odd is [.., 3, 5]); // true
Console.WriteLine(even is [.., 3, 5]); // false
Console.WriteLine(fib is [.., 3, 5]); // true
// </ListPatterns>
}
}In the preceding example, the is operator matches an expression against a property pattern with nested constant and relational patterns.
ms.custom: "updateeachrelease"
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
The is operator can be useful in the following scenarios:
- To check the run-time type of an expression, as the following example shows:
sample · csharpdocs/csharp/language-reference/operators/snippets/shared/IsOperator.csDeclarationPattern
namespace operators;
public static class IsOperator
{
public static void Examples()
{
DeclarationPattern();
IsFirstFridayOfOctober(DateTime.Now);
NullCheck(default(object));
NonNullCheck(new object());
ListPattern();
}
// <IntroExample>
static bool IsFirstFridayOfOctober(DateTime date) =>
date is { Month: 10, Day: <=7, DayOfWeek: DayOfWeek.Friday };
// </IntroExample>
private static void DeclarationPattern()
{
// <DeclarationPattern>
int i = 34;
object iBoxed = i;
int? jNullable = 42;
if (iBoxed is int a && jNullable is int b)
{
Console.WriteLine(a + b); // output 76
}
// </DeclarationPattern>
}
private static void NullCheck(object input)
{
// <NullCheck>
if (input is null)
{
return;
}
// </NullCheck>
}
private static void NonNullCheck(object result)
{
// <NonNullCheck>
if (result is not null)
{
Console.WriteLine(result.ToString());
}
// </NonNullCheck>
}
private static void ListPattern()
{
// <ListPatterns>
int[] empty = [];
int[] one = [1];
int[] odd = [1, 3, 5];
int[] even = [2, 4, 6];
int[] fib = [1, 1, 2, 3, 5];
Console.WriteLine(odd is [1, _, 2, ..]); // false
Console.WriteLine(fib is [1, _, 2, ..]); // true
Console.WriteLine(fib is [_, 1, 2, 3, ..]); // true
Console.WriteLine(fib is [.., 1, 2, 3, _ ]); // true
Console.WriteLine(even is [2, _, 6]); // true
Console.WriteLine(even is [2, .., 6]); // true
Console.WriteLine(odd is [.., 3, 5]); // true
Console.WriteLine(even is [.., 3, 5]); // false
Console.WriteLine(fib is [.., 3, 5]); // true
// </ListPatterns>
}
}The preceding example shows the use of a declaration pattern.
- To check for
null, as the following example shows:
sample · csharpdocs/csharp/language-reference/operators/snippets/shared/IsOperator.csNullCheck
namespace operators;
public static class IsOperator
{
public static void Examples()
{
DeclarationPattern();
IsFirstFridayOfOctober(DateTime.Now);
NullCheck(default(object));
NonNullCheck(new object());
ListPattern();
}
// <IntroExample>
static bool IsFirstFridayOfOctober(DateTime date) =>
date is { Month: 10, Day: <=7, DayOfWeek: DayOfWeek.Friday };
// </IntroExample>
private static void DeclarationPattern()
{
// <DeclarationPattern>
int i = 34;
object iBoxed = i;
int? jNullable = 42;
if (iBoxed is int a && jNullable is int b)
{
Console.WriteLine(a + b); // output 76
}
// </DeclarationPattern>
}
private static void NullCheck(object input)
{
// <NullCheck>
if (input is null)
{
return;
}
// </NullCheck>
}
private static void NonNullCheck(object result)
{
// <NonNullCheck>
if (result is not null)
{
Console.WriteLine(result.ToString());
}
// </NonNullCheck>
}
private static void ListPattern()
{
// <ListPatterns>
int[] empty = [];
int[] one = [1];
int[] odd = [1, 3, 5];
int[] even = [2, 4, 6];
int[] fib = [1, 1, 2, 3, 5];
Console.WriteLine(odd is [1, _, 2, ..]); // false
Console.WriteLine(fib is [1, _, 2, ..]); // true
Console.WriteLine(fib is [_, 1, 2, 3, ..]); // true
Console.WriteLine(fib is [.., 1, 2, 3, _ ]); // true
Console.WriteLine(even is [2, _, 6]); // true
Console.WriteLine(even is [2, .., 6]); // true
Console.WriteLine(odd is [.., 3, 5]); // true
Console.WriteLine(even is [.., 3, 5]); // false
Console.WriteLine(fib is [.., 3, 5]); // true
// </ListPatterns>
}
} When you match an expression against null, the compiler guarantees that no user-overloaded == or != operator is invoked.
- To do a non-null check by using a negation pattern, as the following example shows:
sample · csharpdocs/csharp/language-reference/operators/snippets/shared/IsOperator.csNonNullCheck
namespace operators;
public static class IsOperator
{
public static void Examples()
{
DeclarationPattern();
IsFirstFridayOfOctober(DateTime.Now);
NullCheck(default(object));
NonNullCheck(new object());
ListPattern();
}
// <IntroExample>
static bool IsFirstFridayOfOctober(DateTime date) =>
date is { Month: 10, Day: <=7, DayOfWeek: DayOfWeek.Friday };
// </IntroExample>
private static void DeclarationPattern()
{
// <DeclarationPattern>
int i = 34;
object iBoxed = i;
int? jNullable = 42;
if (iBoxed is int a && jNullable is int b)
{
Console.WriteLine(a + b); // output 76
}
// </DeclarationPattern>
}
private static void NullCheck(object input)
{
// <NullCheck>
if (input is null)
{
return;
}
// </NullCheck>
}
private static void NonNullCheck(object result)
{
// <NonNullCheck>
if (result is not null)
{
Console.WriteLine(result.ToString());
}
// </NonNullCheck>
}
private static void ListPattern()
{
// <ListPatterns>
int[] empty = [];
int[] one = [1];
int[] odd = [1, 3, 5];
int[] even = [2, 4, 6];
int[] fib = [1, 1, 2, 3, 5];
Console.WriteLine(odd is [1, _, 2, ..]); // false
Console.WriteLine(fib is [1, _, 2, ..]); // true
Console.WriteLine(fib is [_, 1, 2, 3, ..]); // true
Console.WriteLine(fib is [.., 1, 2, 3, _ ]); // true
Console.WriteLine(even is [2, _, 6]); // true
Console.WriteLine(even is [2, .., 6]); // true
Console.WriteLine(odd is [.., 3, 5]); // true
Console.WriteLine(even is [.., 3, 5]); // false
Console.WriteLine(fib is [.., 3, 5]); // true
// </ListPatterns>
}
}- To match elements of a list or array by using list patterns. The following code checks arrays for integer values in expected positions:
sample · csharpdocs/csharp/language-reference/operators/snippets/shared/IsOperator.csListPatterns
namespace operators;
public static class IsOperator
{
public static void Examples()
{
DeclarationPattern();
IsFirstFridayOfOctober(DateTime.Now);
NullCheck(default(object));
NonNullCheck(new object());
ListPattern();
}
// <IntroExample>
static bool IsFirstFridayOfOctober(DateTime date) =>
date is { Month: 10, Day: <=7, DayOfWeek: DayOfWeek.Friday };
// </IntroExample>
private static void DeclarationPattern()
{
// <DeclarationPattern>
int i = 34;
object iBoxed = i;
int? jNullable = 42;
if (iBoxed is int a && jNullable is int b)
{
Console.WriteLine(a + b); // output 76
}
// </DeclarationPattern>
}
private static void NullCheck(object input)
{
// <NullCheck>
if (input is null)
{
return;
}
// </NullCheck>
}
private static void NonNullCheck(object result)
{
// <NonNullCheck>
if (result is not null)
{
Console.WriteLine(result.ToString());
}
// </NonNullCheck>
}
private static void ListPattern()
{
// <ListPatterns>
int[] empty = [];
int[] one = [1];
int[] odd = [1, 3, 5];
int[] even = [2, 4, 6];
int[] fib = [1, 1, 2, 3, 5];
Console.WriteLine(odd is [1, _, 2, ..]); // false
Console.WriteLine(fib is [1, _, 2, ..]); // true
Console.WriteLine(fib is [_, 1, 2, 3, ..]); // true
Console.WriteLine(fib is [.., 1, 2, 3, _ ]); // true
Console.WriteLine(even is [2, _, 6]); // true
Console.WriteLine(even is [2, .., 6]); // true
Console.WriteLine(odd is [.., 3, 5]); // true
Console.WriteLine(even is [.., 3, 5]); // false
Console.WriteLine(fib is [.., 3, 5]); // true
// </ListPatterns>
}
}C# language specification
For more information, see The is operator section of the C# language specification and Pattern matching.