ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Use null check instead of a type check succeeding on any not-null value

Last modified: 29 May 2024

The is operator in C# returns false in two cases:

  • when the left side of is is incompatible with the tested type and

  • when the left side of is is null (here is why).

This inspection reports cases when the left side of is is always assignable to the tested type. In such cases, is will evaluate to false only when the left side is null. Therefore, it is suggested to replace the type check with a null check, which will preserve the logic but make readers of the code understand what is actually being tested.

Note that a type check, rather than a null check, may have been the intended condition. If this is the case, you should review your code to determine the underlying issue.