Pattern matching of C × 8

Time:2020-2-25

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

 Is mode 

if (o is Person p) 
Console . WriteLine(p.Name);

 

switch and when 

Print(Person person) 
public bool 
switch (person) 
case Student s when 
return true; 
case Teacher t when 
return false; 
s. Name 
t. Name

 

 

C# 8 InsidePattern Matching 

UseDeconstructor andLocation matching pattern

The following two classesTeacherandStudentBy constructor(ConstructorSumDeconstructor 

public class Teacher 
O references 
public Teacher(string name, 
Name 
name ; 
Subject subject; 
3 references 
string subject) 
public string Name { get; set; } 
2 references 
public string Subject { get; set; } 
O references 
public void Deconstruct(out string name, 
Name ; 
name 
subject 
Subject; 
out string subject)

 

public class Student 
O references 
public Student(string name, 
name 
Name 
Grade 
grade; 
Teacher 
- teacher; 
3 references 
int grade, 
Teacher teacher) 
public string Name { get; set; } 
2 references 
public int Grade { get; set; } 
2 references 
Teacher { get; set; } 
public Teacher 
O references 
public void Deconstruct(out string name, 
out int grade, 
out Teacher teacher) 
name - 
grade 
Name ; 
Grade; 
teacher - 
Teacher;

 

Now I want to write a way to judge whether students are5Grade and Mathematics: 

IsFifthGradeMath(Student student) 
public bool 
return student is Student(

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: 

IsFifthGradeMath(Student student) 
public bool 
return student is Student(

 

If you want to judge whether his teacher teaches mathematics, you can write as follows: 

O references 
IsFifthGradeMath(Student student) 
public bool 
return student is Student( 
5, Teacher(_,

 

Note that all the calls in this section areDeconstructor 

 

In fact, we canDeconstructorRemove the type of, like this: 

IsFifthGradeMath(Student student) 
public bool 
return student is ( 
5 (_,

 

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: 

O references 
IsFifthGradeMath(Student student) 
public bool 
return student is { Grade: 5, Teacher:{ Subject:

I think it will be clear at a glance. Don’t you need to explain? 

 

And this method can be written more generally: 

(object obj) 
public bool 
IsFifthGradeMath 
return obj is Student s 88 
s is { Grade: 5, Teacher:{ Subject:

Parameter type changed toobject, and then judge whether it isStudent, judging whether it is5The grade studies mathematics. 

 

SwtichExpression

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