Pattern Matching and Tuples

As I discussed in my previous post, pattern matching in a new feature in C# 7.0 that provides a more sophisticated way to write selection statements. The situations where sophisticated selections statements more necessary is when selecting on multiple criteria. Often this is seen in other languages when matching against tuples. In an older post, I looked at how Swift uses tuples and pattern matching and used the FizzBuzz problem as a solution. Since C# 7.0 also introduced a better syntax for tuples, I thought would be helpful to revisit the solution, this time in C#.

In the code, the method IsMultipleOf3or5 returns a tuple made of two bools. In this instance, the members of the tuple are given names to make them more readable. This is a big improvement over the old C# tuples that had to be named Item1 and Item2.

The result of IsMultipleOf3or5 is the value that is the subject of the switch statement. Each case statement uses a pattern that names the variable that is being switched. In each case the variable name is status. The when clause then uses the property names defined in the tuple to filter the selection.

Like I said in the previous post, this logic could certainly be implemented with if statements, but the new pattern matching logic makes the code cleaner and easier to read.

March 15, 2017 |
Tags : PatternMatching CSharp

Comments Section

Feel free to comment on the post but keep it clean and on topic.

comments powered by Disqus

About Me

Eric Potter My name is Eric Potter. I have an amazing wife and 5 wonderful children. I am a Microsoft MVP for Developer Tools and Technologies, the Director of Technical Education for Sweetwater in Ft. Wayne Indiana, and an adjunct professor for Indiana Tech. I am a humble toolsmith.

Microsoft MVP Award

pottereric.github.com