How to use pattern matching in C# 8.0


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 . 

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.