Skip to content

Commit 188a130

Browse files
authored
respond to feedback (#11392)
* 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>
1 parent 1906331 commit 188a130

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

docs/csharp/tutorials/pattern-matching.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The most basic toll calculation relies only on the vehicle type:
5555
- A `Bus` is $5.00.
5656
- A `DeliveryTruck` is $10.00
5757

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`.
5959

6060
```csharp
6161
using System;
@@ -81,7 +81,7 @@ namespace toll_calculator
8181
}
8282
```
8383

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.
8585

8686
You can test this code using the following code in `Program.cs`:
8787

@@ -115,7 +115,7 @@ namespace toll_calculator
115115
}
116116
catch (ArgumentException e)
117117
{
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");
119119
}
120120
try
121121
{
@@ -152,13 +152,13 @@ vehicle switch
152152
Car { Passengers: 0} => 2.00m + 0.50m,
153153
Car { Passengers: 1 } => 2.0m,
154154
Car { Passengers: 2} => 2.0m - 0.50m,
155-
Car c when c.Passengers > 2 => 2.00m - 1.0m,
155+
Car c => 2.00m - 1.0m,
156156

157157
// ...
158158
};
159159
```
160160

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.
162162

163163
You would also expand the cases for taxis in a similar manner:
164164

@@ -206,15 +206,15 @@ vehicle switch
206206
};
207207
```
208208

209-
When you've finished, you'll have a method that looks much like the following:
209+
The preceding code shows the `when` clause of a switch 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:
210210

211211
```csharp
212212
vehicle switch
213213
{
214214
Car { Passengers: 0} => 2.00m + 0.50m,
215215
Car { Passengers: 1} => 2.0m,
216216
Car { Passengers: 2} => 2.0m - 0.50m,
217-
Car c when c.Passengers > 2 => 2.00m - 1.0m,
217+
Car c => 2.00m - 1.0m,
218218

219219
Taxi { Fares: 0} => 3.50m + 1.00m,
220220
Taxi { Fares: 1 } => 3.50m,
@@ -366,9 +366,9 @@ Finally, you can remove the two rush hour times that pay the regular price. Once
366366

367367
[!code-csharp[SimplifiedTuplePattern](../../../samples/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs#FinalTuplePattern)]
368368

369-
This example highlights one of the advantages of pattern matching: the pattern branches are evaluated in order. If you rearrange them so that an earlier branch handles one of your later cases, the compiler warns you. Those language rules made it easier to do the preceding simplifications with confidence that the code didn't change.
369+
This example highlights one of the advantages of pattern matching: the pattern branches are evaluated in order. If you rearrange them so that an earlier branch handles one of your later cases, the compiler warns you about the unreachable code. Those language rules made it easier to do the preceding simplifications with confidence that the code didn't change.
370370

371-
Pattern matching provides a natural syntax to implement different solutions than you'd create if you used object-oriented techniques. The cloud is causing data and functionality to live apart. The *shape* of the data and the *operations* on it aren't necessarily described together. In this tutorial, you consumed existing data in entirely different ways from its original function. Pattern matching gave you the ability to write functionality that overrode those types, even though you couldn't extend them.
371+
Pattern matching makes some types of code more readable and offers an alternative to object-oriented techniques when you can'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't necessarily described together. In this tutorial, you consumed existing data in entirely different ways from its original function. Pattern matching gave you the ability to write functionality that overrode those types, even though you couldn't extend them.
372372

373373
## Next steps
374374

docs/csharp/whats-new/csharp-8.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ static string Quadrant(Point p) => p switch
175175

176176
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.
177177

178+
You can explore pattern matching techniques in this [advanced tutorial on pattern matching](../tutorials/pattern-matching.md).
179+
178180
## using declarations
179181

180182
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

Comments
 (0)