Page 22 - MSDN Magazine, May 2019
P. 22
C#
Pattern Matching
in C# 8.0
Filip Ekberg
Over the years we’ve seen a ton of features in C# that improve not only the performance of our code, but more importantly its readability. Given the fast pace of the software industry, the lan- guage certainly needs to keep up and evolve with its user base. Something that’s been widely used in different programming languages, such as Haskell, Swift or Kotlin sometimes find its way into C#. One of these being pattern matching—a concept that has been around for a long time, and something for which a lot of developers in the .NET space have long waited.
As of C# 7.0, developers got a taste of the power that comes with pattern matching. We saw a pattern starting to take form, which later has become an extremely powerful and interesting addition to the language. Just as other language features have drastically changed the way we write our software, I expect that pattern matching in C# will have a similar effect.
Do we really need another language feature, though? Can’t we just use traditional approaches? Of course we could. Although an
addition like pattern matching will most definitely change the way a lot of us choose to write our code, the same question could be said for other language features that have been introduced over the years.
One that changed the C# language drastically was the introduction of Language-Integrated Query (LINQ). Nowadays when processing data, people choose which flavor they personally like. Some choose to use LINQ, which in some cases constructs less-verbose code, while others opt for traditional loops. I expect similar uptake for pattern matching, as the new functionality will change the way developers work as they move away from more verbose approaches. Mind you, the traditional approaches aren’t going anywhere, as many developers will opt to stick with tried-and-true solutions. But the additional language features should offer a way to complement C# code projects, rather than deprecate current code.
Introducing Pattern Matching
If you’ve ever tried languages like Kotlin or Swift, you’ve likely seen examples of pattern matching in action. It’s very commonly used among a lot of different programming languages on the market— mostly, of course, to make the code a bit more readable. So what is pattern matching?
It’s rather simple. You look at a given structure and based on the way it looks, you identify it and you then can immediately use it. If you get a bag of fruit, you look down and immediately see the dif- ference between the apples and the pears. Even if both are green. At the same time, you can look down into your bag of fruit and identify which fruits are green, as we know all fruits have a color.
This article discusses:
• How pattern matching is evolving in C# 8.0
• The new switch expression
• How to leverage tuples together with the pattern matching changes in C# 8.0
Technologies discussed:
C# 8.0, Visual Studio 2019 Preview
16 msdn magazine