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, ReSharper highlights object pattern syntax { }
in null-checking expressions and suggests replace them with the not null
pattern:
If you prefer to use the { }
pattern, you can change the corresponding preferences and ReSharper will help you replace not null
patterns accordingly:
For more information, see Code Syntax Style: Null checking pattern.
Last modified: 30 March 2023