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
You may want to specify the type of the `out` variable for clarity, as shown above. However, the language does support using an implicitly typed local variable:
30
31
32
+
IN OTHER FILE AS WELL
31
33
[!code-csharp[OutVarVariableDeclarations](~/samples/snippets/csharp/new-in-7/program.cs#02_OutVarVariableDeclarations "Implicitly typed Out variable")]
32
34
33
35
* The code is easier to read.
@@ -75,12 +77,14 @@ items:
75
77
76
78
That assignment creates a tuple whose members are `Item1` and `Item2`, which you can use in the same way as <xref:System.Tuple> You can change the syntax to create a tuple that provides semantic names to each of the members of the tuple:
The `namedLetters` tuple contains fields referred to as `Alpha` and `Beta`. Those names exist only at compile time and are not preserved when inspecting the tuple using reflection at runtime, for example.
81
84
82
85
In a tuple assignment, you can also specify the names of the fields on the right-hand side of the assignment:
83
86
87
+
USED IN OTHER FILE
84
88
[!code-csharp[ImplicitNamedTuple](~/samples/snippets/csharp/new-in-7/program.cs#06_ImplicitNamedTuple "Implicitly named tuple")]
85
89
86
90
You can specify names for the fields on both the left and right-hand side of the assignment:
@@ -107,6 +111,7 @@ items:
107
111
108
112
There may be times when you want to unpackage the members of a tuple that were returned from a method. You can do that by declaring separate variables for each of the values in the tuple. This is called *deconstructing* the tuple:
109
113
114
+
USED IN OTHER FILE
110
115
[!code-csharp[CallingWithDeconstructor](~/samples/snippets/csharp/new-in-7/program.cs#10_CallingWithDeconstructor "Deconstructing a tuple")]
111
116
112
117
You can also provide a similar deconstruction for any type in .NET. This is
@@ -116,10 +121,12 @@ items:
116
121
this `Point` class that provides a deconstructor method that extracts
117
122
the `X` and `Y` coordinates:
118
123
124
+
USED IN OTHER FILE
119
125
[!code-csharp[PointWithDeconstruction](~/samples/snippets/csharp/new-in-7/point.cs#11_PointWithDeconstruction "Point with deconstruction method")]
120
126
121
127
You can extract the individual fields by assigning a `Point` to a tuple:
122
128
129
+
USED IN OTHER FILE
123
130
[!code-csharp[DeconstructPoint](~/samples/snippets/csharp/new-in-7/program.cs#12_DeconstructPoint "Deconstruct a point")]
124
131
125
132
You are not bound by the names defined in the `Deconstruct` method. You
@@ -147,6 +154,7 @@ items:
147
154
148
155
The following example defines a `QueryCityDataForYears` method that returns a 6-tuple that contains data for a city for two different years. The method call in the example is concerned only with the two population values returned by the method and so treats the remaining values in the tuple as discards when it deconstructs the tuple.
The `namedLetters` tuple contains fields referred to as `Alpha` and
78
78
`Beta`. Those names exist only at compile time and aren't preserved,
@@ -81,25 +81,25 @@ for example when inspecting the tuple using reflection at runtime.
81
81
In a tuple assignment, you can also specify the names of the fields
82
82
on the right-hand side of the assignment:
83
83
84
-
[!code-csharp[ImplicitNamedTuple](../../../samples/snippets/csharp/new-in-7/program.cs#06_ImplicitNamedTuple"Implicitly named tuple")]
84
+
[!code-csharp[ImplicitNamedTuple](~/samples/snippets/csharp/new-in-7/program.cs#06_ImplicitNamedTuple"Implicitly named tuple")]
85
85
86
86
There may be times when you want to unpackage the members of a tuple that
87
87
were returned from a method. You can do that by declaring separate variables
88
88
for each of the values in the tuple. This unpackaging is called *deconstructing* the tuple:
89
89
90
-
[!code-csharp[CallingWithDeconstructor](../../../samples/snippets/csharp/new-in-7/program.cs#10_CallingWithDeconstructor"Deconstructing a tuple")]
90
+
[!code-csharp[CallingWithDeconstructor](~/samples/snippets/csharp/new-in-7/program.cs#10_CallingWithDeconstructor"Deconstructing a tuple")]
91
91
92
92
You can also provide a similar deconstruction for any type in .NET. You write a `Deconstruct` method as a member of the class. That
93
93
`Deconstruct` method provides a set of `out` arguments for each of the
94
94
properties you want to extract. Consider
95
95
this `Point` class that provides a deconstructor method that extracts
96
96
the `X` and `Y` coordinates:
97
97
98
-
[!code-csharp[PointWithDeconstruction](../../../samples/snippets/csharp/new-in-7/point.cs#11_PointWithDeconstruction"Point with deconstruction method")]
98
+
[!code-csharp[PointWithDeconstruction](~/samples/snippets/csharp/new-in-7/point.cs#11_PointWithDeconstruction"Point with deconstruction method")]
99
99
100
100
You can extract the individual fields by assigning a `Point` to a tuple:
101
101
102
-
[!code-csharp[DeconstructPoint](../../../samples/snippets/csharp/new-in-7/program.cs#12_DeconstructPoint"Deconstruct a point")]
102
+
[!code-csharp[DeconstructPoint](~/samples/snippets/csharp/new-in-7/program.cs#12_DeconstructPoint"Deconstruct a point")]
103
103
104
104
You can learn more in depth about tuples in the
105
105
[tuples article](../tuples.md).
@@ -117,7 +117,7 @@ Discards are supported in the following scenarios:
117
117
118
118
The following example defines a `QueryCityDataForYears` method that returns a 6-tuple that contains a data for a city for two different years. The method call in the example is concerned only with the two population values returned by the method and so treats the remaining values in the tuple as discards when it deconstructs the tuple.
0 commit comments