Microsoft .NET Code Analysis: Use Pattern Matching to Avoid ‘As’ Followed by A ‘Null’ Check

The “is” keyword was introduced in the .NET Framework 2.0 and is considered the recommended approach for avoiding null checks on objects. Here is an example of the issue:

var other = obj as CustomArgumentInstance;

if ((object)other == null)
{
    return false;
}

This is how you fix the code violation:

if (obj is not CustomArgumentInstance other)
{
    return false;
}

The code fix not only improves the cleanliness of your code but also has the potential to enhance performance. By utilizing pattern matching, type checking and extraction are combined, resulting in more efficient execution. These are two significant reasons why implementing this code fix is crucial.

  1. Code readability and conciseness: Pattern matching provides a more expressive and concise syntax compared to using ‘as‘ followed by a ‘null‘ check. It allows you to combine type checking and extraction of properties or values in a single statement, making your code more readable and reducing unnecessary boilerplate code.
  2. Null safety: The ‘as‘ operator in .NET returns null if the cast fails. This requires an additional null check to ensure that the cast was successful before using the result. By using pattern matching, you can avoid this additional null check. Pattern matching allows you to check both the type and perform the extraction in a single step, and if the pattern doesn’t match, the code block is not executed, eliminating the need for a separate null check.

When I setup the IDE0019 code analysis in my .editorConfig it looks like this: dotnet_diagnostic.IDE0019.severity = warning

Summary

During my review of the codebase, I identified 19 instances where this issue occurs. Considering 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

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If 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.

One thought on “Microsoft .NET Code Analysis: Use Pattern Matching to Avoid ‘As’ Followed by A ‘Null’ Check

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.