Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the pattern matching of Clover 8

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to realize the pattern matching of Crank8. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Pattern Matching in C # 8

Use Deconstructor and location matching patterns

The following two classes, Teacher and Student, are both composed of the constructor (Constructor) and Deconstructor:

Next, I want to write a way to judge whether a student is in the fifth grade and is studying math:

Here we use Student's Deconstructor and position matching pattern, where the underscore is called "discard", indicating that we won't use it and don't care about it.

If the code is written in this way, then any student passed in satisfies the match.

If we want to judge students at the age of 5, we can write:

If you also want to judge whether his teacher teaches math or not, you can write:

Notice that Deconstructor is all called here.

In fact, we can remove the type of Deconstructor, like this:

In fact, if statements may be better and easier to maintain in response to this requirement. But if if cannot be used in the case of loop nesting, then this positional pattern matching is useful.

Attribute matching pattern

The location matching pattern is powerful, but the readability is too poor, so let's take a look at the attribute matching pattern.

Or the previous demand:

I think this is clear at a glance, there is no need to explain, right?

And this method can be written in a more general way:

The parameter type is changed to object, and then it is judged whether it is Student or not, and whether it is a math student in the fifth grade or not.

Swtich expression

There are three shaped classes, each with different properties:

Next I'm going to write a method that explicitly corresponds to the information according to different shapes. Here I will use the switch expression of C # 8.

Let's start with the simplest case:

The method parameter is of type object, and then use the switch expression of C # 8, which has a return value, so you can put the return value in the variable and return it.

As you can see, there is no case keyword in the switch expression, but the lambda expression is used here.

This means that if shape is of type Rectangle, then we can access it through the variable r, the string on the right is the value of the case expression, and the return value here is a string.

Write down all three shapes, and that's it:

Notice that there is no break or return here. And the switch expression does not support "fall through".

So how do you add it by default? Here we use the discard matching pattern:

Here we can also use the attribute matching pattern to set the filter criteria:

Using nested switch expressions, you can achieve the same goal:

Finally, we can simplify the whole method a little bit, using the expression-bodied method:

But don't overuse this nested pattern. After all, humans still need to read your code.

Tuple matching pattern

First define an enumeration:

Then write a method, pass in two colors, and get the mixed color:

What is used here is the tuple matching pattern.

At the bottom, it also judges the situation where both colors are the same, as well as all other cases.

The above is the editor for you to share how to achieve the pattern matching of Category 8, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report