Code inspection: Arrange null checking pattern
When checking an expression for null with the type-testing 'is' operator, you can choose between two null-checking patterns:
Use the 'not null' pattern that makes the expression more readable.
Use the object pattern syntax `{ }` that makes the expression more flexible, allowing you to declare a local variable after it.
public static void Test(object? obj)
{
if (obj is not null)
Console.WriteLine("not null");
}
public static void Test(object? obj)
{
if (obj is { })
Console.WriteLine("not null");
}
By default, JetBrains Rider highlights object pattern syntax { }
in null-checking expressions and suggests replace them with the not null
pattern:
![JetBrains Rider syntax style inspection: Use 'not null' pattern JetBrains Rider syntax style inspection: Use 'not null' pattern](https://resources.jetbrains.com.cn/help/img/rider/2024.2/use_not_null_pattern_quick_fix.png)
If you prefer to use the { }
pattern, you can change the corresponding preferences and JetBrains Rider will help you replace not null
patterns accordingly:
![JetBrains Rider syntax style inspection: Use '{ }' pattern JetBrains Rider syntax style inspection: Use '{ }' pattern](https://resources.jetbrains.com.cn/help/img/rider/2024.2/use_object_pattern_quick_fix.png)
For more information, refer to Code Syntax Style: Null checking pattern.
Last modified: 11 February 2024