Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate Patterns Examples #871

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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
9 changes: 6 additions & 3 deletions standard/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ It is an error if *type* is a nullable value type.

> *Example*: The declaration pattern can be used to test values of nullable types: a value of type `Nullable<T>` (or a boxed `T`) matches a type pattern `T2 id` if the value is non-null and `T2` is `T`, or some base type or interface of `T`. For example, in the code fragment
>
> <!-- Example: {template:"standalone-console-without-using", name:"DeclarationPattern1"} -->
> ```csharp
> int? x = 3;
> if (x is int v) { /* code using v */ }
Expand Down Expand Up @@ -98,16 +99,17 @@ Given a *pattern input value* *e* and a constant pattern `P` with converted valu

> *Example*:
>
> <!-- Example: {template:"standalone-console", name:"ConstantPattern1", replaceEllipsis:true, customEllipsisReplacements: ["\"xxx\""], ignoredWarnings:["CS8321"]} -->
> ```csharp
> public static decimal GetGroupTicketPrice(int visitorCount)
> static decimal GetGroupTicketPrice(int visitorCount)
> {
> switch (visitorCount) {
> case 1: return 12.0m;
> case 2: return 20.0m;
> case 3: return 27.0m;
> case 4: return 32.0m;
> case 0: return 0.0m;
> default: throw new ArgumentException();
> default: throw new ArgumentException(...);
> }
> }
> ```
Expand Down Expand Up @@ -160,13 +162,14 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con

> *Example*:
>
> <!-- Example: {template:"standalone-console-without-using", name:"PatternExhaustiveness1", replaceEllipsis:true, customEllipsisReplacements: [""], ignoredWarnings:["CS8321"]} -->
jskeet marked this conversation as resolved.
Show resolved Hide resolved
> ```csharp
> static void M(byte b)
> {
> switch (b) {
> case 0: case 1: case 2: case 3: ... // handle every specific value of byte
> break;
> case byte other: // error: the pattern 'byte other' is subsumed by previous cases because the previous cases are exhaustive for byte
> case byte other: // error: the pattern 'byte other' is subsumed by the (exhaustive) previous cases
> break;
> }
> }
Expand Down
Loading