This article explains new C# 7.0 features that make developers' lives easy, including Is Expression with Patterns and Switch statement with Patterns. Let's explore these features one-by-one with a small example which will help us to understand easily.
Is Expression with Patterns
This is an expression that allows us to add a pattern on the right side of the expression, that can be a value extracted from an expression. In the below code snippet I have added pattern emp, which will extract value from the expression and we can get values like emp.firstName and emp.lastName.
- class Program
- {
- static void Main(string[] args)
- {
- var fulllTimeEmployee = new FullTimeEmployee()
- {
- employeeId = 100001,
- firstName = "Jingeh",
- lastName = "Kumar",
- anualSalary = 5000000
- };
-
- var contractEmployee = new FullTimeEmployee()
- {
- employeeId = 100002,
- firstName = "Abhilash",
- lastName = "Pandey",
- anualSalary = 4500000
- };
-
-
- if (fulllTimeEmployee is Employee emp)
- Console.WriteLine($"This employee is {emp.firstName} + {emp.lastName} full time Empliyee");
- else
- Console.WriteLine("This employee is not full time employee");
- }
-
- }
- public abstract class Employee
- {
- public int employeeId;
- public string firstName;
- public string lastName;
- public abstract double CalculateSalary();
- }
- public class FullTimeEmployee : Employee
- {
- public int anualSalary { get; set; }
- public override double CalculateSalary()
- {
- return this.anualSalary / 12;
- }
- }
-
- public class ContractEmpoyee : Employee
- {
- public int hourlyRate { get; set; }
- public int totalHours { get; set; }
-
- public override double CalculateSalary()
- {
- return this.hourlyRate * this.totalHours;
- }
- }
Switch statement with Patterns
In the context of pattern matching, the switch case statement pattern enhances the case blocks by allowing us to compare the value of switch with the value returned by an expression, rather than a fixed value.
Before the C# 7.0 switch statement. we had to define a specific value and then we had our case statements. To demonstrate this in our current scenario we have a class for a performer, actor, and a dancer.
The below code snippet is for the switch statement in C# 7.0
- class Program
- {
- static void Main(string[] args)
- {
- Performer p = new Performer();
- Dancer d = new Dancer()
- {
- Name = "Michle",
- Age = 30,
- Gender = "Male",
- BestDance = "BestDance"
- };
- Actor a = new Actor()
- {
- Name = "Tom peter",
- Age = 35,
- Gender = "Male",
- BestAward = "FilmFare",
- BestMove = "Harry Potter"
- };
-
-
- switch (a)
- {
- case Performer performer when (performer.Age == 40):
- Console.WriteLine($"The performer {performer.Name}");
- break;
- case Actor actor when (actor.Age == 35):
- Console.WriteLine($"The Actor name is {actor.Name}");
- break;
- case Actor actor:
- Console.WriteLine($"The Actor is unknown");
- break;
- default:
- Console.WriteLine("Not found");
- break;
- case null:
- throw new ArgumentNullException(nameof(a));
- }
- Console.ReadLine();
- }
- }
- public class Performer
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public string Gender { get; set; }
- }
- public class Dancer : Performer
- {
- public string BestDance { get; set; }
- public int Year { get; set; }
- }
-
- public class Actor : Performer
- {
- public string BestMove { get; set; }
- public string BestAward { get; set; }
- }
Now, you can apply switch on any type. Here we have passed Actor object to switch statement.
- Actor a = new Actor();
- switch (a)
- {
-
-
- }
Case clause with pattern & additional condition on them
Here, we have checked additional condition using when clause, based on age. If age is equal to 40 then this case clause executes.
- switch (a)
- {
- case Performer performer when (performer.Age == 40):
- Console.WriteLine($"The performer {performer.Name}");
- break;
-
- }
Default clause and null check
Here are two things to notice while working with the switch statement.
Null case is not unreachable, so don't think that we just step down case by case and hit default and then it never hits null case. If you want to check null condition in the below example then try to pass Actor object as null above switch.
Default clause is always evaluated last.
- Actor a = null;
- switch (a)
- {
- case Performer performer when (performer.Age == 40):
- Console.WriteLine($"The performer {performer.Name}");
- break;
- default:
- Console.WriteLine("Not found");
- break;
- case null:
- throw new ArgumentNullException(nameof(a));
- }
Conclusion
In C# 7.0 enhancement is done in a switch statement and is an expression. Now, you can pass any object in the switch statement and do pattern matching using when clause.
Please visit my previous article (Click Here) to read about more features like Binary Literal, Digit Separator, and Local Function.
Enjoy coding .