|
| 1 | +--- |
| 2 | +title: "is operator - C# reference" |
| 3 | +description: "Learn about the C# is operator that matches an expression against a pattern" |
| 4 | +ms.date: 04/23/2021 |
| 5 | +f1_keywords: |
| 6 | + - "is_CSharpKeyword" |
| 7 | + - "is" |
| 8 | +helpviewer_keywords: |
| 9 | + - "is keyword [C#]" |
| 10 | +ms.assetid: bc62316a-d41f-4f90-8300-c6f4f0556e43 |
| 11 | +--- |
| 12 | +# is operator (C# reference) |
| 13 | + |
| 14 | +In C# 6 and earlier, 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](type-testing-and-cast.md#is-operator) section of the [Type-testing and cast operators](type-testing-and-cast.md) article. |
| 15 | + |
| 16 | +Beginning with C# 7.0, you can also use the `is` operator to match an expression against a pattern, as the following example shows: |
| 17 | + |
| 18 | +:::code language="csharp" source="snippets/shared/IsOperator.cs" id="IntroExample"::: |
| 19 | + |
| 20 | +In the preceding example, the `is` operator matches an expression against a [property pattern](patterns.md#property-pattern) with nested [constant](patterns.md#constant-pattern) and [relational](patterns.md#relational-patterns) patterns. |
| 21 | + |
| 22 | +The `is` operator can be useful in the following scenarios: |
| 23 | + |
| 24 | +- To check the run-time type of an expression, as the following example shows: |
| 25 | + |
| 26 | + :::code language="csharp" source="snippets/shared/IsOperator.cs" id="DeclarationPattern"::: |
| 27 | + |
| 28 | + The preceding example shows the use of a [declaration pattern](patterns.md#declaration-and-type-patterns). |
| 29 | + |
| 30 | +- To check for `null`, as the following example shows: |
| 31 | + |
| 32 | + :::code language="csharp" source="snippets/shared/IsOperator.cs" id="NullCheck"::: |
| 33 | + |
| 34 | + When you match an expression against `null`, the compiler guarantees that no user-overloaded `==` or `!=` operator is invoked. |
| 35 | + |
| 36 | +- Beginning with C# 9.0, you can use a [negation pattern](patterns.md#logical-patterns) to do a non-null check, as the following example shows: |
| 37 | + |
| 38 | + :::code language="csharp" source="snippets/shared/IsOperator.cs" id="NonNullCheck"::: |
| 39 | + |
| 40 | +For the complete list of patterns supported by the `is` operator, see [Patterns](patterns.md). |
| 41 | + |
| 42 | +## C# language specification |
| 43 | + |
| 44 | +For more information, see [The is operator](~/_csharplang/spec/expressions.md#the-is-operator) section of the [C# language specification](~/_csharplang/spec/introduction.md) and the following C# language proposals: |
| 45 | + |
| 46 | +- [Pattern matching](~/_csharplang/proposals/csharp-7.0/pattern-matching.md) |
| 47 | +- [Pattern matching with generics](~/_csharplang/proposals/csharp-7.1/generics-pattern-match.md) |
| 48 | + |
| 49 | +## See also |
| 50 | + |
| 51 | +- [C# reference](../index.md) |
| 52 | +- [C# operators and expressions](index.md) |
| 53 | +- [Patterns](patterns.md) |
| 54 | +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../tutorials/pattern-matching.md) |
| 55 | +- [Type-testing and cast operators](../operators/type-testing-and-cast.md) |
0 commit comments