New C# 7 features in action: Pattern matching in C# 7 – Is expressions with patterns
Patterns in C# 7.0
Constant patterns (c): checks that the input is equal to constant expression
if (creditCard is null) // constant pattern "null" { // Some code }
Type patterns (T x): checks that the input is of type T, and if it is, then extracts the value of the input into a new variable x of type T
if (card is AmericanExpress amex) // type pattern "AmericanExpress amex" { // Some code }
Var patterns (var x): puts the value of the input into a new variable x with the same type as the input
if (creditCard is var card) // var pattern "var card" { // Some code }
In C# 7, pattern matching is introduced to enhance:
- is expressions
- switch statements
Is expression with C# 7’s pattern matching enhancements
Simple Credit Card Processor Class
using System; namespace SampleApplication { public interface ICard { bool IsValidCard { get; set; } } public class AmericanExpress : ICard { public bool IsValidCard { get; set; } public bool ProcessAmexPayment(decimal amount) { throw new NotImplementedException(); } } public class Visa : ICard { public bool IsValidCard { get; set; } public bool IsCreditCard { get; set; } public bool ProcessVisaPayment(decimal amount) { throw new NotImplementedException(); } public bool ProcessVisaCreditCardPayment(decimal amount) { throw new NotImplementedException(); } } public class MasterCard : ICard { public bool IsValidCard { get; set; } public bool IsCreditCard { get; set; } public bool ProcessMasterCardPayment(decimal amount) { throw new NotImplementedException(); } public bool ProcessMasterCreditCardPayment(decimal amount) { throw new NotImplementedException(); } } }
Before C# 7: Sample code to explain the use of is expression in older versions of C#
namespace SampleApplication.CSharp6 { public partial class CSharp6Sample { public static bool DoPayment(object creditCard, decimal amount) { if (creditCard == null) { return false; } if (amount == 0) { return false; } bool success = false; if (creditCard is ICard) { var card = (ICard)creditCard; if (card.IsValidCard == true) { if (card is AmericanExpress) { var amex = (AmericanExpress)creditCard; success = amex.ProcessAmexPayment(amount); } else if (card is Visa) { var visa = (Visa)creditCard; if (visa.IsCreditCard == false) { success = visa.ProcessVisaPayment(amount); } else { success = visa.ProcessVisaCreditCardPayment(amount); } } else if (card is MasterCard) { var masterCard = (MasterCard)creditCard; if (masterCard.IsCreditCard == false) { success = masterCard.ProcessMasterCardPayment(amount); } else { success = masterCard.ProcessMasterCreditCardPayment(amount); } } } } return success; } } }
Modifications to the above sample code to use C# 7’s pattern matching to enhance the is expression
Note the use of pattern matching (in is expression)
- constant pattern
- type pattern
- type pattern with condition
in the below sample code:
namespace SampleApplication.CSharp7 { public partial class CSharp7Sample { public static bool DoPayment(object creditCard, decimal amount) { if (creditCard is null) // constant pattern "null" { return false; } if (amount is 0) // constant pattern "0" { return false; } bool success = false; if (creditCard is ICard card && card.IsValidCard == true) // type pattern "ICard card" with condition { if (card is AmericanExpress amex) // type pattern "AmericanExpress amex" { success = amex.ProcessAmexPayment(amount); } else if (card is Visa visa) { if (visa.IsCreditCard == false) { success = visa.ProcessVisaPayment(amount); } else { success = visa.ProcessVisaCreditCardPayment(amount); } } else if (card is MasterCard masterCard) { if (masterCard.IsCreditCard == false) { success = masterCard.ProcessMasterCardPayment(amount); } else { success = masterCard.ProcessMasterCreditCardPayment(amount); } } } return success; } } }
Happy Coding !!!
.NET Professional | Microsoft Certified Professional | DZone’s Most Valuable Blogger | Web Developer | Author | Blogger
Doctorate in Computer Science and Engineering
Microsoft Certified Professional (MCP) with over 12+ years of software industry experience including development, implementation & deployment of applications in the .NET framework
Experienced and skilled Agile Developer with a strong record of excellent teamwork, successful coding & project management. Specialises in problem identification and proposal of alternative solutions. Provided knowledge and individual mentoring to team members as needed
Among top 3% overall in terms of contribution on Stack Overflow (~2.3 million people reached my posts). Part of the top 1% Stack Overflow answerers in ASP.NET technology.
DZone’s Most Valuable Blogger (MVB)
Created and actively maintain the TechCartNow.com tech blog while also editing, writing, and researching topics for publication.
Excellent skills in Application Development using C#/Vb.Net, .NET Framework, ASP.NET, MVC, ADO.NET, WCF, WPF, Web API, SQL Server, jQuery, Angular, React, BackboneJS