Check out a free preview of the full C# and .NET Basics course
The "Pattern Matching" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:
Spencer explains that pattern matching is useful when dealing with objects of different types and demonstrates how to use the "is" operator to check if a value is of a specific type. He also briefly mentions other types of pattern matching, such as checking for specific content or properties, and mentions that the IDE provides suggestions for using pattern matching.
Transcript from the "Pattern Matching" Lesson
[00:00:00]
>> Spencer Schneidenbach: To touch on pattern matching. So pattern matching is a relatively new feature in C#, so definitely useful. So let's talk about what it means and what it is and why it's important. So there are certain times when you may not know a type at runtime, it may be of one of several types.
[00:00:23]
And this is typically true when you're dealing with objects that may come from different sources, we'll see examples of that further in. But you can do what's called a type pattern, so let me show an example of this. Let me declare, delete all this good stuff, boom, boom, okay?
[00:00:40]
So we can say that if we, for some reason, don't know that this value is string, we may want to check to see if it is an instance of string. So this is the statement that you would use to do that. So is the operator in which tells you is this thing a string or not.
[00:00:59]
And if it is, we can actually declare it and capture this in a variable here. Used to, you had to do this, evaluate string, you have to re-declare the string and re-do what's called casting, which has changed the type as a string. But even then, if you do that It will suggest, hey, you should really use pattern matching for this.
[00:01:24]
So this is just a little bit of what we like to call syntactic sugar, something that developers like to read and like to write that makes their jobs a little easier, makes their eyes a little happier. It's like eye bleach. So I prefer the syntax, re-declaring the variable, right here is just useful, it just captures that type for you so you don't have to worry about re-declaring it and then casting it.
[00:01:49]
So, kinda helpful, super helpful, in fact. There are other types of pattern matching, I'm gonna touch on them. I'm just gonna show you that they exist. They're not stuff that I use a ton of, but they do exist. You can check to see if a value is equal to a specific constant with the if statement.
[00:02:12]
And you might be thinking to yourself, well, that's cool, why wouldn't you just use a double equals, which you could. But it really becomes important when you look at something like this, where you might be determining whether or not a number fits within a certain range. Is it greater than or equal to 0, and is it or, and is it less than or equal to 100?
[00:02:33]
So you can compose these statements multiple ways inside of your pattern matching statement, which is really useful. Another thing to point out is that once you have this variable declared, you can this string variable declared here, you can say and string.length. You can access the string the same way that you could inside of the statement here, so you can continue to operate on it.
[00:03:03]
I do wanna point out, and this is skipping ahead to the operator's part a little bit, that only really applies when you're using what's called a double and. Or basically, a short circuiting and, which we'll talk about. Essentially, that means that if this is false, this statement will not execute.
[00:03:22]
But even if this is false, even if the left side of the statement is false, this statement will still try to execute and C# will complain that this is invalid. Because you basically have said, I want you to execute this even if this is not a string, but I can't have declared this as a string because it's not a string, we've determined that, so little bit of importance.
[00:03:39]
So pattern matching just gives you a little bit better syntax, or a little bit easier syntax to use for for operating on the variables that you have declared inside of your program. This is a really interesting one, property patterns, we'll talk about that. This is skipping ahead just a little bit on classes, but you can say that if you have a person object, a person with the name of John and the age of 30, and is match to see, does this property name equal John and does their age equal 30?
[00:04:17]
In practice, I don't do this a ton, I do this sometimes where I say, if I'm looking at a collection and I wanna say, does this length greater than or equal to 0? But in that case, I usually just write it out, and I simply do that because I think that syntax is more common and I think developers know it more.
[00:04:37]
Lastly, I'll just touch on it, but this is something I don't even think I've written this once. But it's important to know that if you're working on tuples, you can use that if statement and check to see if this tuple is of a certain value. So you could say, is it one, and is this value a string, and is this value equal to true?
[00:04:54]
And if so, the tuple matches the pattern. So a lot of flexibility, I think pattern matching is pretty useful and I think that for the most part, I think that the IDE and the tooling gives you a really good idea of when you can use it. So again, as opposed to re-declaring the,
>> Spencer Schneidenbach: Thing here, the IDE will give you little squiggles and say, hey, you can use pattern matching.
[00:05:23]
It's a good way to discover language features kinda naturally as you're kind of cutting your teeth in C#, it's one of the things I love about the ecosystem.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops