Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/prompts/error-consolidation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Overall steps:

We're going to work through a series of files consolidating errors and warnings.

- For the duration of this chat, all references to "destination file" refer to `property-declaration-errors.md`.
- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to declaring and defining properties.
- For the duration of this chat, all references to "destination file" refer to `pattern-matching-warnings.md`.
- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to list patterns, and other pattern matching features.

The destination file already contains a skeleton for the final output.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ title: Resolve pattern matching errors and warnings
description: There are several pattern matching warnings. Learn how to address these warnings.
f1_keywords:
- "CS8509" # WRN_SwitchNotAllPossibleValues: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '...' is not covered.
- "CS8978"
- "CS8979"
- "CS8980"
- "CS8985"
- "CS9013"
- "CS9060"
- "CS9134"
- "CS9135"
- "CS9335"
- "CS9336"
- "CS9337"
helpviewer_keywords:
- "CS8509"
- "CS8978"
- "CS8979"
- "CS8980"
- "CS8985"
- "CS9013"
- "CS9060"
- "CS9134"
- "CS9335"
- "CS9336"
- "CS9337"
ms.date: 11/07/2025
ms.date: 12/12/2025
ai-usage: ai-assisted
---
# Resolve errors and warnings in pattern matching expressions
Expand All @@ -24,73 +34,59 @@ This article covers the following compiler errors and warnings:
<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
That's by design. The text closely matches the text of the compiler error / warning for SEO purposes.
-->
- [**CS8509**](#incomplete-pattern-matching): *The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '...' is not covered.*
- [**CS8509**](#pattern-completeness-and-redundancy): *The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '...' is not covered.*
- [**CS8978**](#type-pattern-errors): *'...' cannot be made nullable.*
- [**CS8979**](#list-pattern-errors): *List patterns may not be used for a value of type '...'.*
- [**CS8980**](#list-pattern-errors): *Slice patterns may only be used once and directly inside a list pattern.*
- [**CS8985**](#list-pattern-errors): *List patterns may not be used for a value of type '...'. No suitable 'Length' or 'Count' property was found.*
- [**CS9013**](#list-pattern-errors): *A string 'null' constant is not supported as a pattern for '...'. Use an empty string instead.*
- [**CS9060**](#type-pattern-errors): *Cannot use a numeric constant or relational pattern on '...' because it inherits from or extends 'INumberBase&lt;T&gt;'. Consider using a type pattern to narrow to a specific numeric type.*
- [**CS9134**](#switch-expression-syntax-errors): *A switch expression arm does not begin with a 'case' keyword.*
- [**CS9135**](#switch-expression-syntax-errors): *A constant value of type is expected*
- [**CS9335, CS9336**](#redundant-patterns): *The pattern is redundant.*
- [**CS9337**](#redundant-patterns): *The pattern is too complex to analyze for redundancy.*
- [**CS9336**](#pattern-completeness-and-redundancy): *The pattern is redundant.*
- [**CS9337**](#pattern-completeness-and-redundancy): *The pattern is too complex to analyze for redundancy.*

## Switch expression syntax errors

- **CS9134**: *A switch expression arm does not begin with a 'case' keyword.*
- **CS9135**: *A constant value of type is expected*

To write switch expressions correctly, follow the proper syntax rules. For more information, see [Switch expression](../operators/switch-expression.md).
Remove the `case` keyword from switch expression arms. Switch expressions use a different syntax than switch statements (**CS9134**). In switch expressions, each arm consists of a pattern followed by the `=>` token and an expression, without the `case` keyword that's used in switch statements. Use constant values rather than variables in patterns. Pattern matching requires compile-time constants (**CS9135**). Variables can't be used as patterns. The compiler needs to know the exact values at compile time to generate the appropriate matching code.

- Don't use the `case` keyword in switch expressions (**CS9134**). The `case` keyword is used in switch statements, not switch expressions. In a switch expression, each arm consists of a pattern, an optional case guard, the `=>` token, and an expression.
- Use constant values in patterns, not variables (**CS9135**). Pattern matching requires compile-time constants for value patterns.
For more information about the correct syntax, see [Switch expression](../operators/switch-expression.md).

The following example demonstrates the syntax error with `case`:

```csharp
var answer = x switch
{
// CS9134: remove the keyword "case" from each switch arm:
case 0 => false,
case 1 => true,
}
```

To fix this error, remove the `case` keyword:

```csharp
var answer = x switch
{
0 => false,
1 => true,
}
```

## Incomplete pattern matching
## Pattern completeness and redundancy

- **CS8509**: *The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '...' is not covered.*
- **CS9336**: *The pattern is redundant.*
- **CS9337**: *The pattern is too complex to analyze for redundancy.*

To create exhaustive switch expressions, cover all possible input values. For more information, see [Switch expression](../operators/switch-expression.md) and [Switch statement](../statements/selection-statements.md#the-switch-statement).

Add switch arms that handle all possible input values. Use the discard pattern (`_`) to match any remaining values that you don't need to handle explicitly.
Add switch arms that handle all possible input values to create exhaustive switch expressions (**CS8509**). Switch expressions must cover every possible value of the input type. Otherwise the compiler can't guarantee that the expression produces a result for all inputs. Use the discard pattern (`_`) as a final catch-all arm to match any remaining values that you don't need to handle explicitly. The discard pattern ensures that the switch expression handles all possible cases. Review patterns that the compiler identifies as redundant. Redundant patterns can indicate a logic error where you meant to use `not` or different logical operators (**CS9336**). Simplify complex patterns that are too difficult for the compiler to analyze for redundancy. Break them down into simpler, more maintainable expressions (**CS9337**).

The following example generates CS8509:
For more information about exhaustiveness requirements and pattern optimization, see [Switch expression](../operators/switch-expression.md), [Switch statement](../statements/selection-statements.md#the-switch-statement), and [Patterns](../operators/patterns.md).

:::code language="csharp" source="./snippets/pattern-matching-warnings/Switch.cs" id="SwitchNotAllPossibleValues":::
## Type pattern errors

To fix this warning, add a default arm:
- **CS8978**: *'...' cannot be made nullable.*
- **CS9060**: *Cannot use a numeric constant or relational pattern on '...' because it inherits from or extends 'INumberBase&lt;T&gt;'. Consider using a type pattern to narrow to a specific numeric type.*

:::code language="csharp" source="./snippets/pattern-matching-warnings/Switch.cs" id="SwitchAllPossibleValues":::
Use the underlying type directly in patterns when working with types that can't be made nullable (**CS8978**). Types like `System.Nullable<T>`, pointer types, and ref struct types can't be made nullable. You must use the base type in your pattern matching logic.

The `_` pattern matches all remaining values. You can use this pattern to handle invalid or unexpected values.
Use type patterns to narrow generic numeric types to specific numeric types before applying numeric constants or relational patterns (**CS9060**). Generic numeric types that implement `INumberBase<T>` can't be matched directly with numeric constants or relational patterns. The compiler can't determine which specific numeric type is being matched. You must first narrow the value to a concrete numeric type like `int`, `double`, or `decimal`.

## Redundant patterns
For more information about type patterns, see [Nullable value types](../builtin-types/nullable-value-types.md), [Patterns](../operators/patterns.md), and [Generic math](../../../standard/generics/math.md).

- **CS9335, CS9336**: *The pattern is redundant.*
- **CS9337**: *The pattern is too complex to analyze for redundancy.*
## List pattern errors

To write clear pattern matching expressions, avoid redundant patterns. For more information, see [Patterns](../operators/patterns.md).
- **CS8979**: *List patterns may not be used for a value of type '...'.*
- **CS8980**: *Slice patterns may only be used once and directly inside a list pattern.*
- **CS8985**: *List patterns may not be used for a value of type '...'. No suitable 'Length' or 'Count' property was found.*
- **CS9013**: *A string 'null' constant is not supported as a pattern for '...'. Use an empty string instead.*

- Review patterns that the compiler identifies as redundant (**CS9335**, **CS9336**). Redundant patterns can indicate a logic error where you meant to use `not` or different logical operators.
- Simplify complex patterns that are too difficult for the compiler to analyze (**CS9337**). Break down complex patterns into simpler, more maintainable expressions.
Ensure the type supports the required operations for list patterns. List patterns require types that are countable and indexable (**CS8979**, **CS8985**). The type must have an accessible `Length` or `Count` property and support indexing. Runtime types that support list patterns include arrays, `List<T>`, `Span<T>`, and other collection types with appropriate members.

The following example demonstrates a redundant pattern:
Place slice patterns (`..`) directly inside a list pattern. Use them only once per list pattern because they can't appear in nested patterns or outside of list patterns (**CS8980**).

:::code language="csharp" source="./snippets/pattern-matching-warnings/Switch.cs" id="RedundantPattern":::
When matching `Span<char>` or `ReadOnlySpan<char>` types, use an empty string `""` instead of a string null constant. The literal `null` isn't supported as a pattern for span types (**CS9013**).

This warning indicates you likely meant `is not (null or 42)` or `is not (int or string)` instead of using `or` at the top level.
For more information about list pattern requirements and syntax, see [List patterns](../operators/patterns.md#list-patterns) and [Patterns](../operators/patterns.md).
4 changes: 3 additions & 1 deletion docs/csharp/language-reference/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,9 @@ items:
CS8770, CS8774, CS8776, CS8775, CS8777, CS8819, CS8824, CS8825, CS8847
- name: Pattern matching warnings
href: ./compiler-messages/pattern-matching-warnings.md
displayName: CS8509, CS9134, CS9135, CS9335, CS9336, CS9337
displayName: >
CS8509, CS8978, CS8979, CS8980, CS8985, CS9013, CS9060, CS9134, CS9135, CS9335,
CS9336, CS9337
- name: String literal declarations
href: ./compiler-messages/string-literal.md
displayName: >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,10 @@ f1_keywords:
- "CS8974"
- "CS8976"
# C# 11 errors start here
- "CS8978"
- "CS8979"
- "CS8980"
- "CS8984"
- "CS8985"
- "CS8989"
- "CS9011"
- "CS9012"
- "CS9013"
- "CS9018"
- "CS9019"
- "CS9020"
Expand All @@ -468,7 +463,6 @@ f1_keywords:
- "CS9054"
- "CS9056"
- "CS9058"
- "CS9060"
- "CS9064"
- "CS9067"
- "CS9068"
Expand Down