Understanding Pattern matching with C#8

goldytech
4 min readOct 7, 2019

On September 23 2019, Microsoft announce the release of .Net Core 3 and C#8 in a virtual online conference. These two are the major releases. In this post I shall be talking about C#8 enhanced feature of pattern matching, why this because I love functional programming and pattern matching is an important concept in FP languages. Pattern matching was first introduced in in C#7. But C#8 is a major leap forward in this direction.C#8 enables new types of patterns and allows us to use those patterns in even more places. We can now use positional patterns with deconstructors. We can use property patterns to elegantly check the values of multiple properties. Finally we would be seeing how new switch syntax can work with tuples pattern matching.

WHAT IS PATTERN MATCHING

Pattern matching is a functional programming technique to do a type testing. Imagine you receive an anonymous object and you want to identify it type by querying its data. In the below examples you will get the idea of what I’m talking about. But I do encourage you to read this article to get full understanding of this topic.

There are three different types of pattern matching that are introduced in C#8. I will be explaining each of them with examples.

Positional Patterns using Deconstructors

Before we dive into this topic let us first understand our domain model which is in context of HR system. This will help you in better understanding.

So we have very simple model to work with, Employee and Department. Before we can apply the pattern matching techniques on our model class there is one very important thing to remember. You must add deconstructor to the model. A deconstructor is a void method with the name Deconstruct. It accepts the out parameters that you want to reference with the pattern matching. So now our updated Model will be as follows.

Now I want to write a method which accepts object as a parameter and I want to determine whether the passed object is of the employee type and also is Manager working for Human Resources department.

My method would be as follows using the Positional patterns

The code uses pattern matching in parameters in the same order that were defined in the Deconstruct method. Where there is _ (underscore) those parameters are discarded. If I pass the below object to this method it will return true.

If you find this technique as cryptic and not that friendly as far as code readability is concern then hold on there is another way to achieve the same result. Also you may have question in your mind that why this hard way, I can achieve this with simple If. But remember my friend Functional Programming is all about expressions than statements.

Property Pattern

Continuing with the same domain context, we want to find the Manager who works in Human Resources department but with property pattern technique.

This approach is more cleaner and doesn’t require to create a Deconstruct method. If I pass the same employee object as above to this method it will again return me true. You might find it similar to Linq but it works on different concept.

Tuple Patterns

This pattern brings interesting possibilities on the table which can make your code cleaner and precise. We will see the new switch syntax in action too. Let’s shift our domain context from Human Resources to Contract Lifecycle management , CLM for short. In a CLM the contract / agreement transition through various phases which can be demonstrated by the diagram below.

So we have 3 enums here

The ask for us to find the status of the contract given is current status category and its next transition state

Eg if the contract status category is in Request and if the transition happens to Create Agreement then the desired status of the contract should be of Request.

See the entire code below to get the understanding of it.

Notice the change in switch syntax, you won’t find the case keyword. We convert the function input parameters into tuple and then do the tuple pattern matching without the case keyword. Isn’t that cool. You have more expressive code now.

SUMMARY

In this blog post we learned about enhancements that has come in pattern matching feature of C#8. There are also other interesting features released with this version like IAsyncEnumerable, default implementation of methods in interface, Nullable reference types and Static local methods. I encourage you to learn them too and use it in your coding day to day work.

Originally published at http://goldytech.wordpress.com on October 7, 2019.

--

--