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 standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@
- [§11.1](patterns.md#111-general) General
- [§11.2](patterns.md#112-pattern-forms) Pattern forms
- [§11.2.1](patterns.md#1121-general) General
- [§11.2.2](patterns.md#1122-declaration-pattern) Declaration pattern
- [§11.2.3](patterns.md#1123-constant-pattern) Constant pattern
- [§11.2.2](patterns.md#1122-constant-pattern) Constant pattern
- [§11.2.3](patterns.md#1123-declaration-pattern) Declaration pattern
- [§11.2.4](patterns.md#1124-var-pattern) Var pattern
- [§11.2.5](patterns.md#1125-positional-pattern) Positional pattern
- [§11.2.6](patterns.md#1126-property-pattern) Property pattern
Expand Down
2 changes: 1 addition & 1 deletion standard/expressions.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# 12 Expressions

Check warning on line 1 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L1

MDC032::Line length 83 > maximum 81

Check warning on line 2 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L2

MDC032::Line length 84 > maximum 81
## 12.1 General

Check warning on line 3 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L3

MDC032::Line length 85 > maximum 81

Check warning on line 3 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L3

MDC032::Line length 86 > maximum 81

An expression is a sequence of operators and operands. This clause defines the syntax, order of evaluation of operands and operators, and meaning of expressions.

Check warning on line 5 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L5

MDC032::Line length 82 > maximum 81

Check warning on line 6 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L6

MDC032::Line length 82 > maximum 81

Check warning on line 6 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L6

MDC032::Line length 82 > maximum 81
## 12.2 Expression classifications

### 12.2.1 General

The result of an expression is classified as one of the following:

Check warning on line 12 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/expressions.md#L12

MDC032::Line length 82 > maximum 81
- A value. Every value has an associated type.
- A variable. Unless otherwise specified, a variable is explicitly typed and has an associated type, namely the declared type of the variable. An implicitly typed variable has no associated type.
- A null literal. An expression with this classification can be implicitly converted to a reference type or nullable value type.
Expand Down Expand Up @@ -7238,7 +7238,7 @@
- `goto case` statements ([§13.10.4](statements.md#13104-the-goto-statement))
- Dimension lengths in an array creation expression ([§12.8.17.4](expressions.md#128174-array-creation-expressions)) that includes an initializer.
- Attributes ([§23](attributes.md#23-attributes))
- In a *constant_pattern* ([§11.2.3](patterns.md#1123-constant-pattern))
- In a *constant_pattern* ([§11.2.2](patterns.md#1122-constant-pattern))

An implicit constant expression conversion ([§10.2.11](conversions.md#10211-implicit-constant-expression-conversions)) permits a constant expression of type `int` to be converted to `sbyte`, `byte`, `short`, `ushort`, `uint`, or `ulong`, provided the value of the constant expression is within the range of the destination type.

Expand Down
12 changes: 6 additions & 6 deletions standard/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@

// Source: §6.5.4 Definition directives
fragment PP_Declaration
: 'define' PP_Whitespace PP_Conditional_Symbol

Check warning on line 468 in standard/grammar.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/grammar.md#L468

MDC032::Line length 84 > maximum 81
| 'undef' PP_Whitespace PP_Conditional_Symbol

Check warning on line 469 in standard/grammar.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/grammar.md#L469

MDC032::Line length 85 > maximum 81
;

// Source: §6.5.5 Conditional compilation directives
Expand Down Expand Up @@ -647,7 +647,7 @@
nullable_type_annotation
: '?'
;

Check warning on line 650 in standard/grammar.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/grammar.md#L650

MDC032::Line length 86 > maximum 81

// Source: §8.3.1 General
value_type
Expand Down Expand Up @@ -746,7 +746,12 @@
| discard_pattern
;

// Source: §11.2.2 Declaration pattern
// Source: §11.2.2 Constant pattern
constant_pattern
: constant_expression
;

// Source: §11.2.3 Declaration pattern
declaration_pattern
: type simple_designation
;
Expand All @@ -761,11 +766,6 @@
: identifier
;

// Source: §11.2.3 Constant pattern
constant_pattern
: constant_expression
;

// Source: §11.2.4 Var pattern
var_pattern
: 'var' designation
Expand Down Expand Up @@ -998,7 +998,7 @@

predefined_type
: 'bool' | 'byte' | 'char' | 'decimal' | 'double' | 'float' | 'int'
| 'long' | 'object' | 'sbyte' | 'short' | 'string' | 'uint' | 'ulong'

Check warning on line 1001 in standard/grammar.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/grammar.md#L1001

MDC032::Line length 82 > maximum 81
| 'ushort'
;

Expand Down Expand Up @@ -1526,7 +1526,7 @@
group_clause
: 'group' expression 'by' expression
;

Check warning on line 1529 in standard/grammar.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/grammar.md#L1529

MDC032::Line length 83 > maximum 81
query_continuation
: 'into' identifier query_body
;
Expand Down Expand Up @@ -1895,7 +1895,7 @@
goto_statement
: 'goto' identifier ';'
| 'goto' 'case' constant_expression ';'
| 'goto' 'default' ';'

Check warning on line 1898 in standard/grammar.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/grammar.md#L1898

MDC032::Line length 82 > maximum 81
;

// Source: §13.10.5 The return statement
Expand Down
14 changes: 7 additions & 7 deletions standard/patterns.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 11 Patterns and pattern matching

## 11.1 General

Check warning on line 3 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/patterns.md#L3

MDC032::Line length 91 > maximum 81

Check warning on line 4 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/patterns.md#L4

MDC032::Line length 94 > maximum 81
A ***pattern*** may be used with the `is` operator ([§12.14.12](expressions.md#121412-the-is-operator)), in a *switch_statement* ([§13.8.3](statements.md#1383-the-switch-statement)), and in a *switch_expression* ([§12.11](expressions.md#1211-switch-expression)) to describe the shape of data against which incoming data is to be compared. Patterns may be nested, with parts of the data being matched against ***sub-patterns***.

A pattern is tested against a value in a number of contexts:

Check warning on line 7 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/patterns.md#L7

MDC032::Line length 87 > maximum 81

Check warning on line 8 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/patterns.md#L8

MDC032::Line length 91 > maximum 81
- In a *switch_statement*, the *pattern* of a *switch_label* is tested against the *selector_expression* of the *switch_statement*.
- With an *is-pattern* operator, the *pattern* on the right-hand-side is tested against the expression on the left.

Check warning on line 10 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/patterns.md#L10

MDC032::Line length 87 > maximum 81
- In a *switch_expression*, the *pattern* of a *switch_expression_arm* is tested against the expression on the *switch_expression*’s left-hand-side.
- In nested contexts, the *sub-pattern* is tested against values retrieved from properties, fields, or indexed from other input values, depending on the pattern form.

Expand Down Expand Up @@ -72,7 +72,7 @@

The order of evaluation of operations and side effects during pattern-matching (calls to `Deconstruct`, property accesses, and invocations of members of `System.Runtime.CompilerServices.ITuple`) is not specified.

### 11.2.3 Constant pattern
### 11.2.2 Constant pattern

A *constant_pattern* is used to test the value of a pattern input value ([§11.1](patterns.md#111-general)) against the given constant value.

Expand Down Expand Up @@ -115,7 +115,7 @@
>
> *end example*

### 11.2.2 Declaration pattern
### 11.2.3 Declaration pattern

A *declaration_pattern* is used to test that a value has a given type and, if the test succeeds, to optionally provide the value in a variable of that type.

Expand Down Expand Up @@ -143,7 +143,7 @@

> *Example*: Given a type named `var`, the following illustrates the distinction between is-type syntax, an erroneous pattern, and valid *declaration_pattern* spellings.
>
> The `is var` construct (no designation) is the is-type operator ([§12.14.12.1](expressions.md#1214121-the-is-type-operator)) and tests whether the operand's runtime type is `var`:
> The `is var` construct (no designation) is the is-type operator ([§12.14.12.1](expressions.md#1214121-the-is-type-operator)) and tests whether the operands runtime type is `var`:
>
> <!-- Example: {template:"standalone-lib-without-using", name:"VarTypeName1", ignoredWarnings:["CS8981"]} -->
> ```csharp
Expand Down Expand Up @@ -241,7 +241,7 @@

A *var_pattern* cannot be used when there is an in-scope type named `var`.

> *Note*: An escaped identifier such as `@var` or a unicode-escape spelling such as `v\u0061r` names the in-scope type in a *declaration_pattern* but does not spell the contextual `var` token of a *var_pattern*. The patterns `@var y` and `v\u0061r y` are therefore *declaration_pattern*s ([§11.2.2](patterns.md#1122-declaration-pattern)) that match only values whose runtime type is compatible with the in-scope type named `var`. *end note*
> *Note*: An escaped identifier such as `@var` or a unicode-escape spelling such as `v\u0061r` names the in-scope type in a *declaration_pattern* but does not spell the contextual `var` token of a *var_pattern*. The patterns `@var y` and `v\u0061r y` are therefore *declaration_pattern*s ([§11.2.3](patterns.md#1123-declaration-pattern)) that match only values whose runtime type is compatible with the in-scope type named `var`. *end note*

```ANTLR
var_pattern
Expand Down Expand Up @@ -297,7 +297,7 @@
In order to extract the values to match against the patterns in the list,

1. **Tuple form.** If *type* is omitted and the static type of the input value is a tuple type ([§8.3.11](types.md#8311-tuple-types)) or if the input value is a tuple literal ([§12.8.6](expressions.md#1286-tuple-literals)), then this case applies. It is a compile-time error if *n* is not equal to the arity of that tuple type. At runtime, each tuple element is matched against the corresponding *subpattern*; the match succeeds if all of these succeed. If any *subpattern* has an *identifier*, that *identifier* shall name the tuple element at the corresponding position in the tuple type.
2. **Deconstruct form.** Otherwise, if either *type* is present, or *type* is omitted and the static type of the input value contains an accessible `Deconstruct` method ([§12.7](expressions.md#127-deconstruction)), then this case applies. Let *D* be *type* if *type* is present; otherwise let *D* be the static type of the input value. A `Deconstruct` method is selected from *D* using the same overload-resolution rules as for a deconstructing assignment ([§12.23.3](expressions.md#12233-deconstructing-assignment)), with the additional requirement that its number of `out` parameters is equal to *n*; it is a compile-time error if no such method exists. If *type* is present, it is a compile-time error if the static type of the input value is not pattern compatible ([§11.2.2](patterns.md#1122-declaration-pattern)) with *type*; at runtime the input value is tested against *type* and, if that test fails, the positional pattern match fails. Otherwise, the input value is converted to *D* and the selected `Deconstruct` method is invoked with fresh variables receiving its `out` parameters. Each received value is matched against the corresponding *subpattern*, and the match succeeds if all of these succeed. If any *subpattern* has an *identifier*, that *identifier* shall name the parameter at the corresponding position of `Deconstruct`.
2. **Deconstruct form.** Otherwise, if either *type* is present, or *type* is omitted and the static type of the input value contains an accessible `Deconstruct` method ([§12.7](expressions.md#127-deconstruction)), then this case applies. Let *D* be *type* if *type* is present; otherwise let *D* be the static type of the input value. A `Deconstruct` method is selected from *D* using the same overload-resolution rules as for a deconstructing assignment ([§12.23.3](expressions.md#12233-deconstructing-assignment)), with the additional requirement that its number of `out` parameters is equal to *n*; it is a compile-time error if no such method exists. If *type* is present, it is a compile-time error if the static type of the input value is not pattern compatible ([§11.2.3](patterns.md#1123-declaration-pattern)) with *type*; at runtime the input value is tested against *type* and, if that test fails, the positional pattern match fails. Otherwise, the input value is converted to *D* and the selected `Deconstruct` method is invoked with fresh variables receiving its `out` parameters. Each received value is matched against the corresponding *subpattern*, and the match succeeds if all of these succeed. If any *subpattern* has an *identifier*, that *identifier* shall name the parameter at the corresponding position of `Deconstruct`.
3. **ITuple form.** Otherwise, if *type* is omitted, no *subpattern* has an *identifier*, and the static type of the input value is `object`, `System.Runtime.CompilerServices.ITuple`, or a type that has an implicit reference conversion to `System.Runtime.CompilerServices.ITuple`, then this case applies. At runtime, the input value is tested for being a non-`null` instance of `System.Runtime.CompilerServices.ITuple`; if that test fails, the positional pattern match fails. Otherwise, the value’s `Length` property is read and, if it is not equal to *n*, the positional pattern match fails. Otherwise, for each *i* from 1 to *n*, the value obtained by indexing the input value with *i* − 1 is matched against the *i*-th *subpattern*, and the match succeeds if all of these succeed.
4. Otherwise, no case applies and the *positional_pattern* is a compile-time error.

Expand Down Expand Up @@ -397,7 +397,7 @@
> The example declaring `x2` is similar to `if (s is var x2)` in terms of inferring the variable type, but the property pattern guarantees that `x2` is non-null.
> *end note*

Given a match of an expression *e* to the pattern *type* `{` *subpatterns* `}`, it is a compile-time error if the expression *e* is not pattern compatible ([§11.2.2](patterns.md#1122-declaration-pattern)) with the type *T* designated by *type*. If the type is absent, the type is assumed to be the static type of *e*. Each of the identifiers appearing on the left-hand-side of its *subpatterns* shall designate an accessible readable property or field of *T*. If the *simple_designation* of the *property_pattern* is present, it declares a pattern variable of type *T*.
Given a match of an expression *e* to the pattern *type* `{` *subpatterns* `}`, it is a compile-time error if the expression *e* is not pattern compatible ([§11.2.3](patterns.md#1123-declaration-pattern)) with the type *T* designated by *type*. If the type is absent, the type is assumed to be the static type of *e*. Each of the identifiers appearing on the left-hand-side of its *subpatterns* shall designate an accessible readable property or field of *T*. If the *simple_designation* of the *property_pattern* is present, it declares a pattern variable of type *T*.

At runtime, the expression is tested against *T*. If this fails then the property pattern match fails, and the result is `false`. If it succeeds, then each *property_subpattern* field or property is read, and its value matched against its corresponding pattern. The result of the whole match is `false` only if the result of any of these is `false`. The order in which subpatterns are matched is not specified, and a failed match may not test all subpatterns at runtime. If the match succeeds and the *simple_designation* of the *property_pattern* is a *single_variable_designation*, the declared variable is assigned the matched value.

Expand Down Expand Up @@ -458,7 +458,7 @@

Where the syntactic context permits a *pattern*, if the token `_` would resolve as a *simple_name* ([§12.8.4](expressions.md#1284-simple-names)) to an accessible constant or to a type, then `_` is *not* treated as a *discard_pattern*. Instead:

- If `_` resolves to an accessible constant, the `_` is interpreted as a *constant_pattern* ([§11.2.3](patterns.md#1123-constant-pattern)) whose constant expression is that constant.
- If `_` resolves to an accessible constant, the `_` is interpreted as a *constant_pattern* ([§11.2.2](patterns.md#1122-constant-pattern)) whose constant expression is that constant.
- If `_` resolves to a type, then in the right-hand side of an `is` operator the construct *relational_expression* `is _` is interpreted as the is-type operator ([§12.14.12.1](expressions.md#1214121-the-is-type-operator)) testing against that type. In any other syntactic context that admits a *pattern*, a bare `_` resolving to a type is not by itself a valid *pattern*; however, `_` may appear as the *type* of a *declaration_pattern* (e.g., `_ x`) or in other pattern forms that explicitly name a type.

This rule preserves backward compatibility with code that defined `_` as a type or identifier prior to the introduction of the discard pattern. If `_` resolves to anything other than an accessible constant or type (for example, a local variable, parameter, field, or method), the rule does not apply and `_` remains a *discard_pattern*.
Expand Down
Loading