How to use pattern matching in C# 8.0

Take advantage of pattern matching improvements in C# 8.0 to write code that is more readable, maintainable, and efficient

How to use pattern matching in C# 8.0
Thinkstock

Pattern matching is an excellent feature that was introduced in C# 7. You can use pattern matching on any data type, even on custom data types, and use it to extract values from an expression. Pattern matching has been enhanced in C# 8.0, which introduces a wide array of new pattern types. This article presents a discussion of how we can work with pattern matching in C# 8.0.

To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here

Create a console application project in Visual Studio

First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
  4. Click Next. 
  5. In the “Configure your new project” window shown next, specify the name and location for the new project.
  6. Click Create.

This will create a new .NET Core console application project in Visual Studio 2019. We’ll use this project to work with pattern matching in C# 8 in the subsequent sections of this article.

Update your C# language version in Visual Studio

To be able to work with C# 8.0 in Visual Studio, you must use a project that targets .NET Core. So far so good. However, you might also need to change the language version of C# in your project. To do this, follow the steps outlined below:

  1. Right-click on the project.
  2. Select “Properties” to invoke the properties window.
  3. Click Build -> Advanced.
  4. Click on the drop-down control for language version. 
  5. Select C# 8.0 as the language version. 
  6. Click OK.
c 8 visual studio IDG

Figure 1: Selecting C# 8.0 as the programming language for your project in Visual Studio. 

Expressing patterns in C# 8.0

There are three different ways you can express patterns in C# 8.0:

  • Positional patterns
  • Property patterns
  • Tuple patterns

We’ll explore each of these patterns in this section with relevant code examples wherever appropriate.

Positional patterns in C# 8

Positional patterns take advantage of the Deconstruct method on the class and contain nested patterns — hence they are also known as recursive patterns. In a recursive pattern, the output of one expression becomes the input to another expression.

When using the positional pattern, you would typically check for null and then invoke a corresponding Deconstruct method to deconstruct the properties of the object on which the positional pattern is applied into discrete variables.

Let’s understand this with an example. Consider the following class named Rectangle.

public class Rectangle
    {
        public int Length { get; set; }
        public int Breadth { get; set; }
        public Rectangle(int x, int y) => (Length, Breadth) = (x, y);
        public void Deconstruct(out int x, out int y) =>
            (x, y) = (Length, Breadth);
    }

Here’s how you can take advantage of the positional pattern on the Rectangle class.

Rectangle rectangle = new Rectangle(10, 10);
var result = rectangle switch
{
    Rectangle(0, 0) => "The value of length and breadth is zero.",
    Rectangle(10, 10) => "The value of length and breadth is same –
        this represents a square.",
    Rectangle(10, 5) => "The value of length is 10, breadth is 5.",
     _ => "Default."
};
Console.WriteLine(result);

When you execute the above piece of code, you should see the following output in the console window. 

positional pattern in c 8 IDG

Figure 2: Positional pattern matching in action!

Property patterns in C# 8

Property patterns are used to perform a match based on the properties of an object. Consider the following class named Employee.

public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public decimal Salary { get; set; }
        public string Country { get; set; }
    }

The following piece of code demonstrates how you can take advantage of property patterns to compute the income tax of an employee.

public static decimal ComputeIncomeTax
    (Employee employee, decimal salary) =>
        employee switch
    {
        { Country: "Canada" } => (salary * 21)/100,
        { Country: "UAE" } => 0,
        { Country: "India" } => (salary * 30) / 100,
        _ => 0
    };

You can invoke the above piece of code as shown below:

static void Main(string[] args)
        {
            Employee employee = new Employee()
            {
                Id = 1,
                FirstName = "Michael",
                LastName = "Stevens",
                Salary = 5000,
                Country = "Canada"
            };
            decimal incometax = ComputeIncomeTax
            (employee, employee.Salary);
            Console.WriteLine("The income tax is {0}", incometax);
            Console.Read();
        }

When you execute the program, the following output should appear in the console window.

property pattern in c 8 IDG

Figure 3: Property pattern matching in action!

Tuple patterns in C# 8

Tuple patterns are yet another type of pattern that can be used to test multiple pieces of input at the same time. The following piece of code illustrates how tuple patterns can be used.

private static string GetLanguageNames(string team1, string team2)
        => (team1, team2) switch
        {
            ("C++", "Java") => "C++ and Java.",
            ("C#", "Java") => "C# and Java.",
            ("C++", "C#") => "C++ and C#.",
            (_, _) => "Invalid input"
        };
(string, string, string, string) programmingLanguages = ("C++", "Java", "C#", "F#");
var language1 = programmingLanguages.Item1.ToString();
var language2 = programmingLanguages.Item3.ToString();
Console.WriteLine($"The languages selected are: {GetLanguageNames(language1, language2)}");

When you execute the above piece of code, the output should look like this:

tuple pattern in c 8 IDG

Figure 4: Tuple pattern matching in action!

C# 8.0 introduces several enhancements in pattern matching that can help you write code that is more readable, maintainable, and efficient. The new patterns offer a great new way of writing code that should make the lives of C# developers easier for years to come. 

Copyright © 2020 IDG Communications, Inc.

How to choose a low-code development platform