Skip to content
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
22 changes: 22 additions & 0 deletions standard/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ 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`:
>
> ```csharp
> Stream v = OpenDataFile(); // compile-time type of 'v' is 'Stream'
> if (v is string) // compile-time error
> {
> // code assuming v is a string
> }
> ```
>
> However, the following doesn't 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`:
>
> ```csharp
> object v = OpenDataFile();
> if (v is string s)
> {
> // code assuming v is a string
> }
> ```
>
> *end example*

Each pattern form defines the set of values for which the pattern *matches* the value at runtime.

### 11.2.2 Declaration pattern
Expand Down