Hello.
In this video, we're going to talk about Topic 19.
What is pattern matching?
Pattern matching is a technique where you test an expression to determine if it has certain characteristics.
The easiest way to understand popular matching is with an example.
Let's say I want to run some code if some value is no and other if it isn't.
This code is pretty straightforward.
There is only one problem.
We don't know what this object is exactly, and it may turn out that it is some type of quality operator
of an old and that it actually does something else than simply taking if the value is new.
To avoid this, we can use our first pattern from pattern matching the nortec pattern.
Well, it's not rocket science.
This pattern just works if an object is known, but pattern matching can give us much, much more ability.
Let's walk through some more complex examples.
One of the most commonly used buttons is type test.
I want to run some code if a variable is of some type.
Moreover, if it is, I want to cast it to this type.
We felt pattern matching.
I would need to write something like this.
Here I am talking.
If the object is strong, I could do it even simpler by using the East Keyword, which we learned about
before.
So first, I'd check if the object is trying, and then I passed it to string with pattern matching.
I can do it all in one line.
This is quite convenient.
These are string variable will be available only if this object is a string.
So I would not be able to use it anywhere else than inside this if statement.
We can also object some particular properties of the target object.
Here we checked if an object is a pet with weight larger than 10000 and put equal to fish.
All right, the next type of pattern matching is comparing discrete values.
This is very similar to using plain old sweet statement.
Here I have a method.
Taking a string that should represent a number and another string saying what type of number it is.
So, for example, int decimal or float, depending on the second parameter.
I want to convert the first parameter to the given type.
Let's do it with pattern matching.
Please notice this special case.
This is a discard pattern, and it works similarly as default in this which statement.
It will be executed if the type parameter is not equal to any of the specified values.
Let's get two more complex types of pattern matching.
The next one is a relational pattern.
It allows us to check how a given value compares two constants.
As you can see, called, using pattern matching is very simple and readable.
The cool thing about pattern matching is that it has very good visual studio and compiler support.
And we get an error when we do something silly.
For example, let me add some more cases here.
This code doesn't compile because the last two cases are unreachable.
The cases are executed from top to bottom.
So when the eight parameter S10 will hit, the less than 20 case will never reach the less than 11 case.
So what we should do is fix the order of the cases.
Right now, this will work as expected.
This code actually demonstrates one more pattern, no logical pattern.
We used it here.
Where we checked it out is less than 20 and more than 60.
We can also use pattern matching quilted construction.
What we learned about before.
As you can see, we deconstructed the object into its components, and we used them to define a pretty
complex logic.
We could all meet the parameter names.
But personally, I would rather leave them for readability.
As you can see, this also works.
We can also mix the construction with taking particular properties.
All right.
So we learned some of the most basic usages of pattern matching.
There is also a question when to use them and when to use plain old if and switch statements.
In my opinion, you should simply use those that you find more reliable.
You can also mix both to get what's best in any of them.
To summarize, pattern matching is a technique where you can test an expression to determine if it has
certain characteristics.
During the interview, you can be asked about one of the most popular patterns.
How can we check if an object is of a given type and customized to this type in the same line?
For that, we can use pattern matching.
For example, we could write if Param is string text.
This way will cost the around to the string variable called text, but only if this object is of type
strength.
All right.
That's it for this lecture.
Thanks for watching, and I'll see you in the next one.