Pattern Matching Enhancements in C# 9: Exploring New Patterns
Tags: csharp, dotnet, programming, abotwrotethis
Published: 2023-05-10 15:07:45

 As a C# developer, you're probably familiar with pattern matching. This feature allows you to test whether an expression matches a certain pattern and execute different code based on the result. In C# 9, Microsoft introduced several enhancements to pattern matching, including new patterns that make your code more concise and readable. In this blog post, I'll explore some of these new patterns and show you how to use them in your code.

---


## Logical Patterns

The logical patterns _and_, _or_, and _not_ allow you to combine patterns into more complex expressions. For example, suppose you have a method that takes an object and performs different actions depending on its type and value. Using logical patterns, you can define a single _switch_ statement that handles all cases:

```C#
void ProcessObject(object obj)
{
    switch (obj)
    {
        case string s and ({ Length: > 0 } or null):
            Console.WriteLine($"The string is '{s ?? "null"}'");
            break;
        case int i and >= 0:
            Console.WriteLine($"The integer is {i}");
            break;
        case bool b:
            Console.WriteLine($"The boolean is {b}");
            break;
        default:
            Console.WriteLine("Unknown object");
            break;
    }
}
```
In this example, the first case uses the logical pattern and to test whether the object is a non-empty string or null. It also uses the property pattern to check that the string has a length greater than zero. The second case uses the relational pattern >= to test whether the object is a non-negative integer. The third case uses a simple type pattern to test whether the object is a boolean. Finally, the default case handles all other cases.

---


## Relational Patterns

Relational patterns allow you to test whether an expression matches a certain relationship with a value. C# 9 introduces several new relational patterns, including >, <, >=, and <=. For example, suppose you have a method that takes a list of integers and prints the first number that is greater than 10:

```C#
void PrintFirstGreaterThan10(List<int> list)
{
    foreach (var i in list)
    {
        if (i is > 10)
        {
            Console.WriteLine($"The first number greater than 10 is {i}");
            return;
        }
    }

    Console.WriteLine("There are no numbers greater than 10");
}

```
In this example, the is operator is used with the relational pattern >. This pattern tests whether the value of i is greater than 10. If so, it prints the number and returns from the method. Otherwise, it continues to the next number. If there are no numbers greater than 10, the method prints a message indicating that.

---


## Type Patterns

Type patterns allow you to test whether an expression is of a certain type and optionally bind it to a new variable. In C# 9, you can use the or pattern to test whether an expression is of one type or another. For example, suppose you have a method that takes an object and prints its type:

```C#
void PrintType(object obj)
{
    switch (obj)
    {
        case string s or int i:
            Console.WriteLine($"The type of the object is {obj.GetType().Name}");
            break;
        default:
            Console.WriteLine("Unknown type");
            break;
    }
}
```
In this example, the switch statement uses the or pattern to check whether the object is either a string or an int. If the object is a string or an int, the method prints its type. Otherwise, it prints "Unknown type".

---

## Conclusion

Pattern matching is a powerful feature in C# that allows developers to write concise and readable code. C# 9 introduces new patterns, such as logical patterns, relational patterns, and type patterns, which enable even more flexibility in handling different cases in code. By using these patterns, you can simplify your code and make it more expressive.