Skip to content

Commit 1f7e66f

Browse files
Add tuple deconstruction examples (#1360)
* Add tuple deconstruction examples Fixes #768 Adding examples for tuple deconstruction and deconstruction assignment. * Update standard/expressions.md Co-authored-by: Nigel-Ecma <[email protected]> * Add example that can't be inferred Add an additional example where the types can't be inferred. * fix example templates. --------- Co-authored-by: Nigel-Ecma <[email protected]>
1 parent 29c09f4 commit 1f7e66f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

standard/expressions.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ An expression `E` is ***deconstructed*** to a tuple expression with `n` elements
12631263
- Otherwise, `E` cannot be deconstructed.
12641264
12651265
Here, `__v` and `__v1, ..., __vn` refer to otherwise invisible and inaccessible temporary variables.
1266+
12661267
> *Note*: An expression of type `dynamic` cannot be deconstructed. *end note*
12671268
12681269
## 12.8 Primary expressions
@@ -1600,6 +1601,35 @@ A *tuple_expression* is classified as a tuple.
16001601

16011602
A *deconstruction_expression* `var (e1, ..., en)` is shorthand for the *tuple_expression* `(var e1, ..., var en)` and follows the same behavior. This applies recursively to any nested *deconstruction_tuple*s in the *deconstruction_expression*. Each identifier nested within a *deconstruction_expression* thus introduces a declaration expression ([§12.17](expressions.md#1217-declaration-expressions)). As a result, a *deconstruction_expression* can only occur on the left side of a simple assignment.
16021603

1604+
> *Example*:
1605+
> The following code declares three variables: a, b, and c. Each of which is an integer and is assigned its value from the tuple on the right hand side of the assignment.
1606+
>
1607+
> <!-- Example: {template:"standalone-console-without-using", name:"DiscardExpressions1"} -->
1608+
> ```csharp
1609+
> var (a, b, c) = (1, 2, 3); // a is 1, b is 2, and c is 3.
1610+
> var sum = a + b + c; // sum is 6.
1611+
> ```
1612+
>
1613+
> Any of the individual elements of the assignment can itself be a deconstruction expression. For example, the following deconstruction expression assigns six variables, `a` through `f`.
1614+
>
1615+
> <!-- Example: {template:"standalone-console-without-using", name:"DiscardExpressions2"} -->
1616+
> ```csharp
1617+
> var (a, b, (c, d, (e, f))) = (1, 2, (3, 4, (5, 6)));
1618+
> ```
1619+
>
1620+
> In this example, notice that the structure of nested tuples must match on both sides of the assignment.
1621+
>
1622+
> If the variable(s) on the left side are implicitly typed, the corresponding expression must have a type:
1623+
>
1624+
> <!-- Example: {template:"standalone-console-without-using", name:"DiscardExpressions3",expectedErrors:["CS8130","CS8130", "CS8130"]} -->
1625+
> ```csharp
1626+
> (int a, string? b) = (42, null); //OK
1627+
> var (c, d) = (42, null); // Invalid as type of d cannot be inferred
1628+
> (int e, var f) = (42, null); // Invalid as type of f cannot be inferred
1629+
> ```
1630+
>
1631+
> *end example*
1632+
16031633
A tuple expression has a type if and only if each of its element expressions `Ei` has a type `Ti`. The type shall be a tuple type of the same arity as the tuple expression, where each element is given by the following:
16041634
16051635
- If the tuple element in the corresponding position has a name `Ni`, then the tuple type element shall be `Ti Ni`.

0 commit comments

Comments
 (0)