Skip to content

Wrong information about pattern matching switch statement #1585

Closed

Description

@paulomorgado

In [https://docs.microsoft.com/en-us/dotnet/articles/csharp/csharp-7#pattern-matching], the documentation states that:

The code above adds cases for 0 as a special case of int, and null as a special case when there is no input. This demonstrates one important new feature in switch pattern expressions: the order of the case expressions now matters. The 0 case must appear before the general int case. Otherwise, the first pattern to match would be the int case, even when the value is 0.

Switching the case 0 with case int val will be a CS8120 The switch case has already been handled by a previous case.

What works is adding the guard when val != 0 to case int val.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions