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
* respond to feedback
I've been watching feedback on the pattern matching tutorial. This address that feedback.
Also, it adds a link from the "what's new in C# 8" article.
* Great review suggestion.
Co-Authored-By: BillWagner <wiwagn@microsoft.com>
Copy file name to clipboardExpand all lines: docs/csharp/tutorials/pattern-matching.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ The most basic toll calculation relies only on the vehicle type:
55
55
- A `Bus` is $5.00.
56
56
- A `DeliveryTruck` is $10.00
57
57
58
-
Create a new `TollCalculator` class, and implement pattern matching on the vehicle type to get the toll amount.
58
+
Create a new `TollCalculator` class, and implement pattern matching on the vehicle type to get the toll amount. The following code shows the initial implementation of the `TollCalculator`.
59
59
60
60
```csharp
61
61
usingSystem;
@@ -81,7 +81,7 @@ namespace toll_calculator
81
81
}
82
82
```
83
83
84
-
The preceding code uses a **switch expression** (not the same as a [`switch`](../language-reference/keywords/switch.md) statement) that tests the **type pattern**. A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. Finally, the `null` pattern catches when `null` is passed to this method. The `null` pattern can be last because the other type patterns match only a non-null object of the correct type.
84
+
The preceding code uses a **switch expression** (not the same as a [`switch`](../language-reference/keywords/switch.md) statement) that tests the **type pattern**. A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The `{ }` case must follow the cases for each vehicle type. If the order were reversed, the `{ }` case would take precedence. Finally, the `null` pattern detects when a`null` is passed to this method. The `null` pattern can be last because the other type patterns match only a non-null object of the correct type.
85
85
86
86
You can test this code using the following code in `Program.cs`:
87
87
@@ -115,7 +115,7 @@ namespace toll_calculator
115
115
}
116
116
catch (ArgumentExceptione)
117
117
{
118
-
Console.WriteLine("Caught an argument exception when using the wrong type", DayOfWeek.Friday);
118
+
Console.WriteLine("Caught an argument exception when using the wrong type");
119
119
}
120
120
try
121
121
{
@@ -152,13 +152,13 @@ vehicle switch
152
152
Car { Passengers: 0} =>2.00m+0.50m,
153
153
Car { Passengers: 1 } =>2.0m,
154
154
Car { Passengers: 2} =>2.0m-0.50m,
155
-
Carcwhenc.Passengers>2=>2.00m-1.0m,
155
+
Carc=>2.00m-1.0m,
156
156
157
157
// ...
158
158
};
159
159
```
160
160
161
-
The first three cases test the type as a `Car`, then check the value of the `Passengers` property. If both match, that expression is evaluated and returned. The final clause shows the `when` clause of a switch arm. You use the `when` clause to test conditions other than equality on a property. In the preceding example, the `when` clause tests to see that there are more than 2 passengers in the car. Strictly speaking, it's not necessary in this example.
161
+
The first three cases test the type as a `Car`, then check the value of the `Passengers` property. If both match, that expression is evaluated and returned.
162
162
163
163
You would also expand the cases for taxis in a similar manner:
Theprecedingcodeshowsthe `when` clauseofaswitch arm. You use the `when` clause to test conditions other than equality on a property. When you've finished, you'll have a method that looks much like the following:
210
210
211
211
```csharp
212
212
vehicle switch
213
213
{
214
214
Car { Passengers: 0} =>2.00m+0.50m,
215
215
Car { Passengers: 1} =>2.0m,
216
216
Car { Passengers: 2} =>2.0m-0.50m,
217
-
Carcwhen c.Passengers > 2 => 2.00m - 1.0m,
217
+
Carc=>2.00m-1.0m,
218
218
219
219
Taxi { Fares: 0} =>3.50m+1.00m,
220
220
Taxi { Fares: 1 } =>3.50m,
@@ -366,9 +366,9 @@ Finally, you can remove the two rush hour times that pay the regular price. Once
Patternmatchingmakessometypesofcodemorereadableandoffersanalternativetoobject-orientedtechniqueswhenyoucan't add code to your classes. The cloud is causing data and functionality to live apart. The *shape* of the data and the *operations* on it aren'tnecessarilydescribedtogether. Inthistutorial, youconsumedexistingdatainentirelydifferentwaysfromitsoriginalfunction. Patternmatchinggaveyoutheabilitytowritefunctionalitythatoverrodethosetypes, eventhoughyoucouldn'textendthem.
The discard pattern in the preceding switch matches when either `x` or `y`, but not both, is 0. A switch expression must either produce a value or throw an exception. If none of the cases match, the switch expression throws an exception. The compiler generates a warning for you if you do not cover all possible cases in your switch expression.
177
177
178
+
You can explore pattern matching techniques in this [advanced tutorial on pattern matching](../tutorials/pattern-matching.md).
179
+
178
180
## using declarations
179
181
180
182
A **using declaration** is a variable declaration preceded by the `using` keyword. It tells the compiler that the variable being declared should be disposed at the end of the enclosing scope. For example, consider the following code that writes a text file:
0 commit comments