C# 7.0 introduces pattern matching. Well, compared to other features, this require a little bit of explanation.
There is many types of pattern matching and three are supported in C# 7: Type, Const, Var.
If you used the is
keyword before, you know it test for a certain type. However, you still needed to cast the variable if you wanted to use it. That alone made the is
statement completely irrelevant and people preferred to cast and check for null rather than check for types.
C# 6.0 - Type Pattern Matching (before)
1 | public void Something(object t) |
C# 7.0 - Type Pattern Matching
1 | public void Something(object t) |
The difference with Type Pattern Matching
This saves you one line in a pattern that is common and repetitive way too often.
More pattern matching
C# 7.0 - Const Pattern Matching
Const
pattern is basically checking for specific value. That includes null
check. Other constant may also be used.
1 | public void Something(object t) |
C# 7.0 - Var pattern Matching
This is a bit more weird and may look completely pointless since it will not match any types.
However, when you couple it with the when
keyword… it where magic starts coming.
1 | private int[] invalidValues = [1,4,7,9]; |
Of course, this example is trivial but add some real-life Line Of Business applications and you end up with a very versatile of putting incoming values into the proper bucket.
Are you going to use it?
When new features are introduced in a language, I like to ask people whether it’s a feature they would use.
So please leave me a comment and let me know if it’s something that will simplify your life or, at least, your code.