DEV Community

Alex Dhaenens
Alex Dhaenens

Posted on

What the hell is positional pattern matching?

With the arrival of the brand new C# 8.0, a lot of new things are possible! One of my personal favorites is the positional pattern matching feature!
Just like my other posts about C#, I'll be using the Elf class which has two properties: Age, which is the age and ElfishName, which is the name of the elf in elfish. Before we dive into this feature, let me give you a brief refresher of some stuff you need to know in order to understand what is happening.

Background

Firsly, as you might know, in C# 7.0 there is a feature called pattern matching (if not you can read about it here). Pattern matching makes it possible to do a type check and a cast directly in one line:

if (person is Elf)
Enter fullscreen mode Exit fullscreen mode

Also in c# 7.0 you can extract properties out of an object in one line. This feature is called object deconstructing (you can learn more about this here) and it looks like this:

(int age, string elfishName) = elf;
Enter fullscreen mode Exit fullscreen mode

It is a nice feature but, as you might remember, it requires a deconstructor. A deconstructor is a method called Deconstruct in a class and looks like this:

public void Deconstruct(out int age, out string elfishName)
        {
            age = Age;
            elfishName = ElfishName;
        }
Enter fullscreen mode Exit fullscreen mode

What is it?

In C# 8.0 however, you can combine both pattern matching & object deconstructing which is called positional pattern matching. It is important to note is that just as deconstructing it requires a deconstructor.

So what does it do? Well, it makes it possible to type check the variable, then cast it to a specific type and then deconstruct it IN ONE LINE! It looks something like this:

if (person is Elf(int age, string elfishName))
            {
                Console.WriteLine($"Elf with elfish name{elfishName} is {age} years old");
            }
Enter fullscreen mode Exit fullscreen mode

The Age and ElfishName properties are extracted into the age and elfishName variables, just like in object deconstructing. This is a nice shorthand, but don't forget that a deconstructor is required!

An extra addition is this:

           if (person is Elf(110, string elfishName))
            {
                Console.WriteLine($"Elf with elfish name{elfishName} is 110 years old");
            }
Enter fullscreen mode Exit fullscreen mode

In this piece of code, I've already provided a const value (in this case 110) against which the casted object is checked. So what this one line actually does, is firstly it checks if the person is an Elf, casts it, then deconstructs the casted object and lastly checks if the Age property is equal to 110. You can then access the ElfishName of that casted object in the elfishName variable. Isn't this nice??

Source

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#positional-patterns
Demo project: https://bitbucket.org/aldhaenens/p-d-demo/src/master/

Oldest comments (0)