Web
Analytics
C# 9.0 new pattern matching feature | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

C# 9.0 new pattern matching feature

What is Pattern Matching

  • checks if a value has a certain shape.
  • C# 9.0 Introduce relational patterns like >,<,>=,<=,not, and, or etc.

if (obj is Vendor{ YearOfBirth: >= 1983} VendorDOB1983) {

// Use the VendorDOB1983 variable here }

using System;

namespace ConsoleApp13
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee.PatternMatchingEnhancements(20);
            Console.ReadLine();

        }
    }
    public class Employee
    {
        
        public static void PatternMatchingEnhancements(object character)
        {
            var isLetter = character is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') and not (>= '0' and <= '9');
            Console.WriteLine($"{character} is a letter and not a number: {isLetter}");
        }
    }
}