You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/deconstruct.md
+8-9Lines changed: 8 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
-
title: Deconstructing tuples and other types | Microsoft Docs
3
-
description: Learn how to deconstruct tuples and other types
4
-
keywords: .NET,.NET Core, C#0
2
+
title: Deconstructing tuples and other types
3
+
description: Learn how to deconstruct tuples and other types.
4
+
keywords: .NET,.NET Core,C#
5
5
author: rpetrusha
6
6
ms-author: ronpet
7
7
ms.date: 07/18/2016
@@ -11,7 +11,6 @@ ms.technology: devlang-csharp
11
11
ms.devlang: csharp
12
12
ms.assetid: 0b0c4b0f-4a47-4f66-9b8e-f5c63b195960
13
13
---
14
-
15
14
# Deconstructing tuples and other types #
16
15
17
16
A tuple provides a light-weight way to retrieve multiple values from a method call. But once you retrieve the tuple, you have to handle its individual elements. Doing this on an element-by-element basis is cumbersome, as the following example shows. The `QueryCityData` method returns a 3-tuple, and each of its elements is assigned to a variable in a separate operation.
@@ -38,22 +37,22 @@ There are two ways to deconstruct a tuple:
38
37
39
38
- You can use the `var` keyword so that C# infers the type of each variable. You place the `var` keyword outside of the parentheses. The following example uses type inference when deconstructing the 3-tuple returned by the `QueryCityData` method.
Note that you cannot specify a specific type outside the parentheses even if every field in the tuple has the
50
-
same type. This generates compiler error CS8136, "`var (...)` form disallows a specific type for `var`.
49
+
same type. This generates compiler error CS8136, "Deconstruction 'var (...)' form disallows a specific type for 'var'.".
51
50
52
51
Note that you must also assign each element of the tuple to a variable. If you omit any elements, the compiler generates error CS8132, "Cannot deconstruct a tuple of 'x' elements into 'y' variables."
53
52
54
53
## Deconstructing tuple elements with discards
55
54
56
-
Often when deconstructing a tuple, you're interested in the values of only some elements. Starting with C# 7, you can take advantage of C#'s support for *discards*, which are write-only variables whose values you've chosen to ignore. A discard is designated by an underscore character ("_") in an assignment. You can discard as many values as you like; all are represented by the single discard, `_`.
55
+
Often when deconstructing a tuple, you're interested in the values of only some elements. Starting with C# 7, you can take advantage of C#'s support for *discards*, which are write-only variables whose values you've chosen to ignore. A discard is designated by an underscore character ("\_") in an assignment. You can discard as many values as you like; all are represented by the single discard, `_`.
57
56
58
57
The following example illustrates the use of tuples with discards. The `QueryCityDataForYears` method returns a 6-tuple with the name of a city, its area, a year, the city's population for that year, a second year, and the city's population for that second year. The example shows the change in population between those two years. Of the data available from the tuple, we're unconcerned with the city area, and we know the city name and the two dates at design-time. As a result, we're only interested in the two population values stored in the tuple, and can handle its remaining values as discards.
59
58
@@ -85,7 +84,7 @@ The overloaded `Deconstruct` method in the following example illustrates one pos
85
84
86
85
## Deconstructing a user-defined type with discards
87
86
88
-
Just as you do with [tuples](#deconstructing-tuple-elements-with-discards), you can use discards to ignore selected items returned by a `Deconstruct` method. Each discard is defined by a variable named "_", and a single deconstruction operation can include multiple discards.
87
+
Just as you do with [tuples](#deconstructing-tuple-elements-with-discards), you can use discards to ignore selected items returned by a `Deconstruct` method. Each discard is defined by a variable named "\_", and a single deconstruction operation can include multiple discards.
89
88
90
89
The following example deconstructs a `Person` object into four strings (the first and last names, the city, and the state) but discards the last name and the state.
0 commit comments