C# 7.0 feature Part I : Pattern matching


In this series of posts, I will explain the new features of C# 7.0

Let us see the Pattern Matching feature in the current post.

There are currently 2 existing language constructs which has been enhanced with patterns by Microsoft team:

  1. with the keyword is
  2. With Switch case

Before starting let us see some advantages of Pattern matching:

  • To match patterns on any data type, even on custom data types
  • Built-in pattern matching
  • Pattern matching can extract values from your expression

Okay so let us see the switch case first.

Assume we have a class called Customer which has been implemented by 2 classes, Agent and DirectCosumer as shown below:

 class Customer
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string City { get; set; }

 class Agent : Customer

class DirectConsumer : Customer

With C# 7.0 pattern matching feature, we can:

  • write additional conditions in case statements
  • switch on any type
  • use patterns in case statement

So we can write switch case statements as below:

   case Agent a when (a.CustomerId == 11):
   Console.WriteLine($"Customer is an agent and Name: {a.Name}");

   case DirectConsumer b when ((b.CustomerId == 21) & (b.City == "Pune"):
   Console.WriteLine($"Customer is a consumer(Pune location) and Name: {b.Name}");

   Console.WriteLine("Customer Not found");

   case Null:
   throw new ArgumentNullException(nameof(shape));

There are few points which are very important:

  • Now the sequence of the case statement matters as the first which satisfies the condition will be processed just like catch clauses
  • Even though you put default first before null case, it will first check whether it fits with null and if it does not then only it goes for default case so default will be executed at the last
  • If customer object is null, processing will fall into the Null case, even if it is an Agent or DirectConsumer null instance

Now let us see keyword is:

You might be wondering that is keyword is there in C# since the beginning then why it is called as a new feature?

Well till now we could use is keyword either to check if a specified interface is implemented, or if the type of the object derives from a base class.

From C# 7.0 onwards, we can use is with:

  • Type pattern:

With type pattern, we can check whether the object is compatible or not:

  1. if (a is Agent p) Console.WriteLine($"it's an agent: {p.Name}");
  2. if (d is DirectConsumer b && (b.City == "Pune")) Console.WriteLine($"it's a direct cosumer lives in {b.City}");
  • Const pattern:

It can be used to verify any constant numbers or Null:

  1. if (a is null) throw new ArgumentNullException(nameof(a));
  2. if (a is 10) Console.WriteLine("it is 10");


Let us take some example to check whether the object is a string:

object obj = "Hello, World!";
if (obj is string str)
One more example where we want to check whether an object is equal to some constants:
object obj = 1; 

if (obj is 1)

Hope it helps 🙂





One thought on “C# 7.0 feature Part I : Pattern matching

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s