How to use pattern matching in C#

Sample matching is a terrific attribute initial released in C# 7. You can use pattern matching on any data sort, even on customized data types, to extract values from an expression.

Sample matching was enhanced in C# eight, which released a huge array of new pattern types. It was enhanced even even more in C# nine, with the addition of new relational and sensible patterns. This short article talks about the recently added patterns in C# nine and how we can choose advantage of them, with code illustrations where ever suitable.

To function with the code illustrations presented in this short article, you really should have Visual Studio 2019 set up in your program. If you never previously have a duplicate, you can download Visual Studio 2019 right here.

Develop a console software job in Visual Studio

Very first off, let us generate a .Web Main console software job in Visual Studio. Assuming Visual Studio 2019 is set up in your program, abide by the techniques outlined beneath to generate a new .Web Main console software job in Visual Studio.

  1. Start the Visual Studio IDE.
  2. Click on “Create new job.”
  3. In the “Create new project” window, decide on “Console Application (.Web Main)” from the list of templates displayed.
  4. Click Future.
  5. In the “Configure your new project” window, specify the name and site for the new job.
  6. Click Develop.

We’ll use this job to check out the new pattern matching characteristics in C# nine in the subsequent sections of this short article.

Relational patterns in C# nine

Relational patterns enable the use of the relational operators fewer than <, fewer than or equivalent to <=, larger than >, and larger than or equivalent to >=.

Copyright © 2021 IDG Communications, Inc.