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 stris always an instance of System.String, so the only purpose of case string myStr is to check whether str is null.
That's why JetBrains Rider 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.
Before
voidTest(string str)
{
switch(str)
{
casestring myStr:
// use myStr
break;
}
}
After
voidTest(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.
note
This inspection suggests that you use a C# 8.0 feature.
By default, JetBrains Rider automatically detects C# version based on the associated compiler. However, you can specify the target C# version explicitly for a project — press on the project in the Solution Explorer and use the Language version selector on the Application page of the Project Properties dialog .
To set the C# version for all projects in your solution, specify it in a Directory.Build.props file in your solution directory as described here.