Pattern matching was introduced in the C# programming language with the release of C# 7 and has since become the preferred approach for checking the shape or structure of data. Since its inception, pattern matching has gained widespread usage in coding. Here is an example of code written prior to the introduction of pattern matching in C#:
if (result.ObjectType == ProjectTypes.Boolean ||
result.ObjectType == ProjectTypes.String)
{
return (result.ToString() == "true");
}
Here is how you would refactor this using pattern matching:
if (result.ObjectType is ProjectTypes.Boolean
or ProjectTypes.String)
{
return (result.ToString() == "true");
}
Pattern matching is much cleaner and easier to understand. Here are other reasons why pattern matching is better:
- Readability and expressiveness: Pattern matching provides a more natural and readable way to express conditional logic and make decisions based on the shape or structure of data. It allows you to match values against patterns, such as types, properties, or other structural elements, making the code more intuitive and easier to understand.
- Concise syntax: Pattern matching introduces a concise syntax that combines type checking and extraction of properties or elements in a single operation. This reduces the need for boilerplate code and enhances code brevity.
- Flexibility and extensibility: Pattern matching in .NET is extensible and allows you to define custom patterns for user-defined types. This enables you to handle specific scenarios or patterns that are relevant to your domain or application, providing greater flexibility and power.
- Switch expressions: Pattern matching is closely related to switch expressions, which were introduced in C# 8.0. Switch expressions leverage pattern matching to provide a more concise and expressive way to handle branching logic based on different patterns or conditions.
- Deconstruction and tuple patterns: Pattern matching supports deconstruction and tuple patterns, enabling you to easily extract and match elements from tuples or deconstructible types. This simplifies the process of working with complex data structures and improves code clarity.
- Enhanced error handling: Pattern matching can be utilized for exception handling scenarios. It allows you to catch specific exceptions based on their types or additional properties, leading to more fine-grained error handling and error recovery strategies.
When I setup the IDE0078 code analysis in my .editorConfig it looks like this:
dotnet_diagnostic.IDE0078.severity = warning
Summary
During my review of the codebase for this article, I identified a significant number of 89 instances where this issue occurs. Considering the magnitude of the refactoring required, it is essential to streamline the process. Tools like CodeRush from DevExpress offer valuable extensions that simplify the refactoring task with a single mouse click, making the process efficient and convenient.
For further guidance and insights, I highly recommend obtaining a copy of my book, “Rock Your Code: Coding Standards for Microsoft .NET” available on Amazon.com. Additionally, to explore more performance tips for .NET, I encourage you to acquire the 3rd edition of “Rock Your Code: Code & App Performance for Microsoft .NET” also available on Amazon.com.
To analyze your code using the same settings I used in these articles, I encourage you to incorporate my EditorConfig file. It can be found at the following link: https://bit.ly/dotNetDaveEditorConfig. I update this file quarterly, so remember to keep yours up to date as well. I hope you will check out my OSS project Spargine by using this link: https://bit.ly/Spargine.
Please feel free to leave a comment below. I would appreciate hearing your thoughts and feedback.
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

One thought on “Microsoft .NET Code Analysis: Use Pattern Matching”