|
| 1 | +--- |
| 2 | +title: "Deconstruction expression - extract properties or fields from a tuple or other type" |
| 3 | +description: "Learn about deconstruction expressions: expressions that extract individual properties or fields from a tuple or user defined type into discrete expressions." |
| 4 | +ms.date: 12/17/2024 |
| 5 | +--- |
| 6 | +# Deconstruction expression - Extract properties of fields from a tuple or other user-defined type |
| 7 | + |
| 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: |
| 9 | + |
| 10 | +:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="TupleDeconstruction"::: |
| 11 | + |
| 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`. |
| 13 | + |
| 14 | +## Tuple deconstruction |
| 15 | + |
| 16 | +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: |
| 17 | + |
| 18 | +:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="TupleDeconstructionWithDiscard"::: |
| 19 | + |
| 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"::: |
| 23 | + |
| 24 | +## Record deconstruction |
| 25 | + |
| 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. |
| 27 | + |
| 28 | +The `record` shown in the following code declares two positional properties, `SquareFeet` and `Address`, along with another property, `RealtorNotes`: |
| 29 | + |
| 30 | +:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="RecordDeconstruction"::: |
| 31 | + |
| 32 | +When you deconstruct a `House` object, all positional properties, and only positional properties, are deconstructed, as shown in the following example: |
| 33 | + |
| 34 | +:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="RecordDeconstructionUsage"::: |
| 35 | + |
| 36 | +You can make use of this behavior to specify which properties of your record types are part of the compiler-synthesized `Deconstruct` method. |
| 37 | + |
| 38 | +## Declare `Deconstruct` methods |
| 39 | + |
| 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. |
| 41 | + |
| 42 | +The following code declares a `Point3D` struct that has two `Deconstruct` methods: |
| 43 | + |
| 44 | +:::code language="csharp" source="./snippets/shared/Deconstruction.cs" id="StructDeconstruction"::: |
| 45 | + |
| 46 | +The first method supports deconstruction expressions that extract all three axis 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. |
| 47 | + |
| 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. |
| 49 | + |
| 50 | +Multiple `Deconstruct` methods are allowed as long as the compiler can determine one unique `Deconstruct` method for a deconstruction expression. Typically, multiple `Deconstruct` methods for the same type have different numbers of parameters. You can also create multiple `Deconstruct` methods that differ by parameter types. However, in many cases, too many `Deconstruct` methods can lead to ambiguity errors and misleading results. |
| 51 | + |
| 52 | +## C# language specification |
| 53 | + |
| 54 | +For more information, see the deconstruction section of the [C# Standard](~/_csharpstandard/standard/expressions.md#127-deconstruction). |
| 55 | + |
| 56 | +## See also |
| 57 | + |
| 58 | +- [C# operators and expressions](index.md) |
| 59 | +- [Tuple types](../builtin-types/value-tuples.md) |
| 60 | +- [Records](../builtin-types/record.md) |
| 61 | +- [Structure types](../builtin-types/struct.md) |
0 commit comments