Skip to content

Commit e499e84

Browse files
committed
proofread and edit
1 parent a05ea3a commit e499e84

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

docs/csharp/language-reference/operators/deconstruction.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@ ms.date: 12/17/2024
55
---
66
# Deconstruction expression - Extract properties of fields from a tuple or other user defined type
77

8-
A *deconstruction expression* deconstructs data fields in an instance of an object. Each discrete data element is stored in a distinct variable, as shown in the following example:
8+
A *deconstruction expression* extracts data fields from an instance of an object. Each discrete data element is written to a distinct variable, as shown in the following example:
99

1010
:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="TupleDeconstruction":::
1111

12-
The preceding code snippets creates a [tuple](../builtin-types/value-tuples.md) that has two integer values, `X` and `Y`. The second statement *deconstructs* that tuple and stores the tuple elements in discrete variables `x`, and `y`.
12+
The preceding code snippet creates a [tuple](../builtin-types/value-tuples.md) that has two integer values, `X` and `Y`. The second statement *deconstructs* that tuple and stores the tuple elements in discrete variables `x`, and `y`.
1313

1414
## Tuple deconstruction
1515

1616
All [tuple types](../builtin-types/value-tuples.md) support deconstruction expressions. Tuple deconstruction extracts all the tuple's elements. If you only want some of the tuple elements, use a [discard](../tokens/discard.md) for the unused tuple members, as shown in the following example:
1717

1818
:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="TupleDeconstructionWithDiscard":::
1919

20-
In the preceding example, the `Y` and `label` members are discarded. You can specify multiple discards in the same deconstruction expression.
20+
In the preceding example, the `Y` and `label` members are discarded. You can specify multiple discards in the same deconstruction expression. You can use discards for all the members of the tuple. The following example is legal, although not useful:
21+
22+
:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="AllDiscards":::
2123

2224
## Record deconstruction
2325

24-
All [record](../builtin-types/record.md) types that have a [primary constructor](../builtin-types/record.md#positional-syntax-for-property-definition) support deconstruction. The compiler synthesizes a deconstruct method that extracts all properties declared in the primary constructor. Deconstruction for records doesn't extract properties that aren't declared using the primary constructor syntax.
26+
[Record](../builtin-types/record.md) types that have a [primary constructor](../builtin-types/record.md#positional-syntax-for-property-definition) support deconstruction for positional parameters. The compiler synthesizes a `Deconstruct` method that extracts the properties synthesized from positional parameters in the primary constructor. The compiler synthesized `Deconstruction` method doesn't extract properties declared as properties in the record type.
2527

2628
The `record` shown in the following code declares two positional properties, `SquareFeet` and `Address`, along with another property, `RealtorNotes`:
2729

@@ -35,17 +37,17 @@ You can make use of this behavior to specify which properties of your record typ
3537

3638
## Declare `Deconstruct` methods
3739

38-
You can add deconstruct support to any class, struct, or interface you declare. You declare one or `Deconstruct` methods in your type, or as extension methods on that type. A deconstruction expression can resolve to a unique method `void Deconstruct(out var p1, ..., out var pn)`. The `Deconstruct` method can be either an instance method or an extension method. The type of each parameter in the `Deconstruct` method must match the type of the corresponding argument in the deconstruct method. The deconstruction expression assigns the value of each argument to the value of the corresponding `out` parameter in the `Deconstruct` method. If multiple `Deconstruct` methods match the deconstruction expression, the compiler reports an ambiguity.
40+
You can add deconstruction support to any class, struct, or interface you declare. You declare one or `Deconstruct` methods in your type, or as extension methods on that type. A deconstruction expression calls a method `void Deconstruct(out var p1, ..., out var pn)`. The `Deconstruct` method can be either an instance method or an extension method. The type of each parameter in the `Deconstruct` method must match the type of the corresponding argument in the deconstruction expression. The deconstruction expression assigns the value of each argument to the value of the corresponding `out` parameter in the `Deconstruct` method. If multiple `Deconstruct` methods match the deconstruction expression, the compiler reports an error for the ambiguity.
3941

4042
The following code declares a `Point3D` struct that has two `Deconstruct` methods:
4143

4244
:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="StructDeconstruction":::
4345

44-
The first method supports deconstruction expressions that extract all three axes values: `X`, `Y`, and `Z`. The second method supports deconstructing only the planar values, `X` and `Y`. The first method has an *arity* of 3, the second has an arity of 2.
46+
The first method supports deconstruction expressions that extract all three axes values: `X`, `Y`, and `Z`. The second method supports deconstructing only the planar values: `X` and `Y`. The first method has an *arity* of 3; the second has an arity of 2.
4547

46-
The preceding section described the compiler synthesized `Deconstruct` method for `record` types with a primary constructor. You can declare additional `Deconstruct` methods in record types. These can either add additional properties, remove some of the default properties, or both. You can also declare a `Deconstruct` that matches the compiler synthesized signature. If you declare such a `Deconstruct` method, the compiler won't synthesize one.
48+
The preceding section described the compiler synthesized `Deconstruct` method for `record` types with a primary constructor. You can declare more `Deconstruct` methods in record types. These methods can either add other properties, remove some of the default properties, or both. You can also declare a `Deconstruct` that matches the compiler synthesized signature. If you declare such a `Deconstruct` method, the compiler doesn't synthesize one.
4749

48-
In most cases, multiple `Deconstruct` methods for the same type have different arities. This isn't a strict requirement. As long as the compiler can determine one unique `Deconstruct` method for a deconstruction expression, the multiple `Deconstruct` methods are allowed. However, in many cases, too many `Deconstruct` methods can lead to ambiguity errors and misleading results.
50+
Typically, multiple `Deconstruct` methods for the same type have different numbers of parameters. You can create multiple `Deconstruct` methods providing the signatures have different parameter types. As long as the compiler can determine one unique `Deconstruct` method for a deconstruction expression, the multiple `Deconstruct` methods are allowed. However, in many cases, too many `Deconstruct` methods can lead to ambiguity errors and misleading results.
4951

5052
## C# language specification
5153

docs/csharp/language-reference/operators/snippets/shared/Deconstruction.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public static void Examples()
2323
var (x2, _, _) = tuple2;
2424
// </TupleDeconstructionWithDiscard>
2525

26+
// <AllDiscards>
27+
var (_, _, _) = tuple2;
28+
// </AllDiscards>
29+
2630
// <RecordDeconstructionUsage>
2731
var house = new House(1000, "123 Coder St.")
2832
{

docs/csharp/language-reference/tokens/discard.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ The `_` character services as a *discard*, which is a placeholder for an unused
99

1010
There are two uses for the *discard* token:
1111

12-
1. To declare a variable with a storage location that won't ever be read. You may need to declare a variable that you don't intend to read or otherwise use its value:
12+
1. To declare an unused variable. A discard can't be read or accessed.
1313
- Unused `out` arguments: `var r = M(out int _, out var _, out _);`
1414
- Unused lambda expression parameters: `Action<int> _ => WriteMessage();`
1515
- Unused deconstruction arguments: `(int _, var answer) = M();`
16-
1. In a [discard pattern](../operators/patterns.md#discard-pattern) to match any expression. You'll usually add a `_` pattern to satisfy exhaustiveness requirements.
16+
1. To match any expression in a [discard pattern](../operators/patterns.md#discard-pattern). You can add a `_` pattern to satisfy exhaustiveness requirements.
1717

1818
The `_` token is a valid identifier in C#. The `_` token is interpreted as a discard only when no valid identifier named `_` is found in scope.
1919

docs/csharp/language-reference/tokens/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ ms.assetid: 4c5c0539-2e37-40b7-91ce-75af5aabd3f9
1616

1717
# C# Special Characters
1818

19-
Special characters are predefined, contextual characters that modify the program element (a literal string, an identifier, or an attribute name) to which they are prepended. C# supports the following special characters:
19+
Special characters are predefined, contextual characters that modify the program element (a literal string, an identifier, or an attribute name) to which they're prepended. C# supports the following special characters:
2020

2121
- [@](./verbatim.md), the verbatim identifier character.
2222
- [$](./interpolated.md), the interpolated string character.
2323
- ["""](./raw-string.md), A sequence of three or more `"` characters provides the delimiters for a raw string literal.
2424
- [_](./discard.md), a `_` represents a *discard*, a placeholder for an unused variable.
2525

26-
This section only includes those tokens that are not operators. See the [operators](../operators/index.md) section for all operators.
26+
This section only includes those tokens that aren't operators. See the [operators](../operators/index.md) section for all operators.

0 commit comments

Comments
 (0)