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:
- with the keyword is
- 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:
switch(customer) { case Agent a when (a.CustomerId == 11): Console.WriteLine($"Customer is an agent and Name: {a.Name}"); break; case DirectConsumer b when ((b.CustomerId == 21) & (b.City == "Pune"): Console.WriteLine($"Customer is a consumer(Pune location) and Name: {b.Name}"); break; default: Console.WriteLine("Customer Not found"); break; 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:
-
if (a is Agent p) Console.WriteLine($"it's an agent: {p.Name}");
-
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:
-
if (a is null) throw new ArgumentNullException(nameof(a));
-
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)
{
Console.WriteLine(str);
}
object
obj = 1;
if
(obj
is
1)
{
Console.WriteLine(
true
);
}
Hope it helps 🙂
One thought on “C# 7.0 feature Part I : Pattern matching”