Conditional Pattern Matching in C#

C# version 7.0 provides a unique feature called Conditional Pattern Matching that makes your code more concise and readable. Using this feature, you can match certain members of any object to certain conditions and values. In this article, you will learn conditional pattern matching techniques in C#.


Conditional Pattern Matching in C# | C# Pattern Matching

In general, pattern matching refers to the code where you can decide the code execution flow conditionally by comparing specific values with the specified input pattern. Earlier, you have used either constant values or any return value of any functions to compare objects or values. Now, pattern mating allows you to compare objects in the following ways.

1. Null patterns.
2. Negated patterns.
3. Type patterns.
4. Property pattern.
5. Constant pattern.

Lets look at each one with C# code example.


using System;  
using System.IO;  
  
namespace PatternMatchingDemo
{  
    class OrderInvoice
    {  
        public int OrderId { get; set; }  
        public string CustomerName { get; set; }  
        public int Quantity { get; set; }
        public int Discount{ get; set; }
        public int TotalAmount { get; set; }  
    }
}

Constant Pattern:

using System;  
using PatternMatchingDemo;
  
namespace PatternMatchingApp
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var orderInvoice = new OrderInvoice() { OrderId = 1, CustomerName = "Michel Mishra", Discount = 0, TotalAmount = 1000 };  
            if (orderInvoice.Discount is 0)  
            {
                Console.WriteLine($"{orderInvoice.CustomerName} has no discount.");
            }
        }  
    }  
}  

Null Pattern:

using System;  
using PatternMatchingDemo;
  
namespace PatternMatchingApp
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var orderInvoice = new OrderInvoice() { OrderId = 1, Discount = 0, TotalAmount = 1000 };  
            if (orderInvoice.CustomerName is null)  
            {
                Console.WriteLine($"The order ID:{orderInvoice.OrderID} is missing customer name.");
            }
        }  
    }  
}  

Negated Pattern:

using System;  
using PatternMatchingDemo;
  
namespace PatternMatchingApp
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var orderInvoice = new OrderInvoice() { OrderId = 1, CustomerName = "Michel Mishra", Discount = 0, TotalAmount = 1000 };  
            if (orderInvoice.CustomerName is not null)  
            {
                Console.WriteLine($"The order ID:{orderInvoice.OrderID} is contains customer name.");
            }
        }  
    }  
}  

Type Pattern:

using System;  
using PatternMatchingDemo;
  
namespace PatternMatchingApp
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var orderInvoice = new OrderInvoice() { OrderId = 1, CustomerName = "Michel Mishra", Discount = 0, TotalAmount = 1000 };  
            if (orderInvoice is OrderInvoice oi)  
            {
                Console.WriteLine($"The order ID:{orderInvoice.OrderID} is generated for customer {oi.CustomerName}.");
            }
        }  
    }  
}  

Property Pattern:

using System;  
using PatternMatchingDemo;
  
namespace PatternMatchingApp
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var orderInvoice = new OrderInvoice() { OrderId = 1, CustomerName = "Michel Mishra", Discount = 10, TotalAmount = 1000 };  
            if (orderInvoice is {Discount:10}) //Discount = 10%  
            {
                Console.WriteLine($"The order ID:{orderInvoice.OrderID} got {orderInvoice.Discount}% discount.");
            }

            if (orderInvoice is {Discount: >10}) //Discount = 10%  
            {
                Console.WriteLine($"The order ID:{orderInvoice.OrderID} got grater than {orderInvoice.Discount}% discount.");
            }
        }  
    }  
}  


– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo