DEV Community

Nick
Nick

Posted on

Record and Pattern Matching in C# 9

C# 9, the latest version of the widely-used programming language, introduces powerful new features that can greatly simplify your code and make it more expressive. Two of the most exciting additions are C# Record and Pattern Matching.

C# Record is a new reference type that provides a concise and immutable way of declaring classes. It combines the benefits of classes and structs, offering built-in equality, immutability, and value-based equality semantics. With Records, you no longer need to write boilerplate code for equality checks. The compiler generates it for you, making your code cleaner and more maintainable.

To define a record in C# 9, you simply use the record keyword followed by the name of the record and its properties. Here's an example that defines a Person record with Name and Age properties:

public record Person(string Name, int Age);
Enter fullscreen mode Exit fullscreen mode

With this small amount of code, you automatically get features such as value-based equality, value-based comparison, pattern matching, and deconstructing. The generated equality and comparison methods compare all properties by default, making it easy to check if two instances of a record are equal.

Pattern Matching is another powerful feature introduced in C# 9 that allows you to perform complex pattern-based operations on your data. It simplifies conditional checks and enables you to extract data from objects in a more concise and readable way.

An example of pattern matching is using the is keyword to check if an object matches a particular pattern. Let's say you have a list of people, and you want to find all the adults:

List<Person> people = GetPeople();

foreach (var person in people)
{
    if (person is Person { Age: >= 18 })
    {
        Console.WriteLine($"{person.Name} is an adult");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this code snippet, we iterate over the list of People and use pattern matching to check if each person is an adult (age greater than or equal to 18). We can access the Age property directly within the pattern, simplifying the code and making it more readable.

Pattern matching also works well with records. You can easily check if two records have the same values for certain properties:

Person person1 = new Person("John", 25);
Person person2 = new Person("John", 25);

if (person1 is person2)
{
    Console.WriteLine("person1 and person2 have the same values");
}
Enter fullscreen mode Exit fullscreen mode

In this example, the is pattern matches person2 against person1, allowing us to determine if both records have the same values for all properties.

C# 9's Record and Pattern Matching features provide a powerful combination for simplifying and enhancing your code. They offer concise syntax, improved readability, and reduced boilerplate code. By using these features, you can write cleaner and more maintainable code, saving time and effort in the long run. So, dive into C# 9 and start taking advantage of these exciting new features in your projects!

Top comments (0)