JetBrains Rider 2024.3 Help

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

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.

public static void Print(string str) { if (str is string) Console.WriteLine(str); }

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.

Last modified: 29 May 2024