Skip to content

Commit

Permalink
Add example annotation to 3 new examples (dotnet#1025)
Browse files Browse the repository at this point in the history
* Add example annotation

* Add example annotation
  • Loading branch information
RexJaeschke authored Nov 30, 2023
1 parent 38d9fe5 commit 4d398ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions standard/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ A *declaration_pattern* and a *var_pattern* can result in the declaration of a l

Each pattern form defines the set of types for input values that the pattern may be applied to. A pattern `P` is *applicable to* a type `T` if `T` is among the types whose values the pattern may match. It is a compile-time error if a pattern `P` appears in a program to match a pattern input value ([§11.1](patterns.md#111-general)) of type `T` if `P` is not applicable to `T`.

> *Example*: The following example generates a compile-time error because the compile-time type of `v` is `Stream`. A variable of type `Stream` can never have a value that is reference compatible with `string`:
> *Example*: The following example generates a compile-time error because the compile-time type of `v` is `TextReader`. A variable of type `TextReader` can never have a value that is reference-compatible with `string`:
>
> <!-- Example: {template:"standalone-console", name:"PatternFormGen1", expectedWarnings:["CS0184"]} -->
> ```csharp
> Stream v = OpenDataFile(); // compile-time type of 'v' is 'Stream'
> TextReader v = Console.In; // compile-time type of 'v' is 'TextReader'
> if (v is string) // compile-time error
> {
> // code assuming v is a string
> }
> ```
>
> However, the following doesnt generate a compile-time error because the compile-time type of `v` is `object`. A variable of type `object` could have a value that is reference compatible with `string`:
> However, the following doesnt generate a compile-time error because the compile-time type of `v` is `object`. A variable of type `object` could have a value that is reference-compatible with `string`:
>
> <!-- Example: {template:"standalone-console", name:"PatternFormGen2"} -->
> ```csharp
> object v = OpenDataFile();
> object v = Console.In;
> if (v is string s)
> {
> // code assuming v is a string
Expand Down
1 change: 1 addition & 0 deletions standard/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ The definite-assignment state of *v* at the beginning of a case’s guard clause
> *Example*: The second rule eliminates the need for the compiler to issue an error if an unassigned variable is accessed in unreachable code. The state of *b* is “definitely assigned” in the unreachable switch label `case 2 when b`.
>
> <!-- Example: {template:"standalone-console-without-using", name:"DefAssignSwitch", expectedWarnings:["CS0162"]} -->
> ```csharp
> bool b;
> switch (1)
Expand Down

0 comments on commit 4d398ce

Please sign in to comment.