C# 7 InsidePattern Mathing
For more information, please refer to the official document: https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8 “more patterns in more places” wt.mc “id = dt-mvp-5003302
switch and when
C# 8 InsidePattern Matching
UseDeconstructor andLocation matching pattern
The following two classesTeacherandStudentBy constructor（ConstructorSumDeconstructor：
Now I want to write a way to judge whether students are5Grade and Mathematics:
Here we use itStudentOfDeconstructorMatch the pattern with the position. The underline is called“Discard “(discard), which means we won’t use it and don’t care about it.
If the code writes like this, then any student passing in will satisfy the match.
If you want to judge5For students of age, we can write as follows:
If you want to judge whether his teacher teaches mathematics, you can write as follows:
Note that all the calls in this section areDeconstructor。
In fact, we canDeconstructorRemove the type of, like this:
In fact, for this kind of requirement, using if statement may be better and easier to maintain. But it cannot be used if the loop is nestedif, then this location pattern matching is useful.
Property matching pattern
The location matching pattern is very powerful, but the readability is too poor. Let’s look at the attribute matching pattern。
Or previous needs:
I think it will be clear at a glance. Don’t you need to explain?
And this method can be written more generally:
Parameter type changed toobject, and then judge whether it isStudent, judging whether it is5The grade studies mathematics.
There are three shape classes with different properties:
Now I’m going to write a method that explicitly displays the corresponding information according to different shapes. I’ll use C here# 8 Of switchExpression.
Start with the simplest case:
The type of method parameter isobject, and then use C# 8OfswitchExpression, the expression has a return value, so you can put the return value in a variable and return it.
As you can see, there is no switch expressioncase Keyword, which is used herelambdaExpression.
The meaning of this sentence is that ifshapeyesRectangleType, then we can access it through the R variable. The string on the right is thiscaseThe value of the expression, where the return value is a string.
Write all three shapes, that’s it:
Notice, there’s nobreakperhapsreturn。 alsoswitchExpression does not support add in（fall through）。
How to add the default? Here we useDiscard matchPattern:
Here we can also useProperty matching patternTo set the filter conditions:
Using nested switchesExpression can also achieve the same purpose:
In the end, we can simplify the whole method a little bit and useexpression-bodiedMethod:
But don’t overuse this nested pattern. After all, humans still need to read your code…
Tuple matching pattern
Define an enumeration first:
Then write a method and pass in two colors to get the mixed color:
The tuple matching pattern is used.
At the bottom, we also judge the same color and all other situations.
For more details, please refer to the official document: https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8 “more patterns in more places” wt.mc “id = dt-mvp-5003302