ReSharper 2024.1 Help

Code inspection: The source expression always matches the provided pattern

In the example below, case string myStr looks like a type check, but it actually does two things:

  • Checks that str in an instance of System.String.

  • Checks that str is not null.

But according to the method signature str is always an instance of System.String, so the only purpose of case string myStr is to check whether str is null.

That's why ReSharper notifies you that the type pattern used in the matching expression does not show the real semantic of the code, which is just a null check.

For null checking in pattern matching expressions, C# 8 introduces an object pattern with the following syntax: { }. This pattern is designed to match everything except null values and it is suggested in a quick-fix.

void Test(string str) { switch (str) { case string myStr: // use myStr break; } }
void Test(string str) { switch (str) { case { } myStr: // use myStr break; } }

Having an object pattern instead of a type pattern when the type of expression does not change gives you the following advantages:

  • Everyone who reads the code will see that this is just a null check.

  • If you ever refactor the code in the initial example and change the type of the str parameter to object for example, the code will continue to compile but will perform a type check, and you might not notice it until you see changes in code behavior at runtime. { } myStr on the other hand will always do a null check and behave similarly regardless of the type of str.

Last modified: 08 April 2024