Take advantage of pattern matching improvements in C# 8.0 to write code that is more readable, maintainable, and efficient Credit: 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. Launch the Visual Studio IDE. Click on “Create new project.” In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed. Click Next. In the “Configure your new project” window shown next, specify the name and location for the new project. 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: Right-click on the project. Select “Properties” to invoke the properties window. Click Build -> Advanced. Click on the drop-down control for language version. Select C# 8.0 as the language version. Click OK. 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. 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. 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: 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. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages news F# 9 adds nullable reference types Latest version of Microsoft’s functional .NEt programming language provides a type-safe way to handle reference types that can have null as a valid value. By Paul Krill Nov 18, 2024 3 mins Microsoft .NET Programming Languages Software Development news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos