ReSharper 2024.3 Help

Code inspection: Expression is always 'null'

This inspection reports expressions that are known to always evaluate to null based on value and nullability analysis.

ReSharper suggests replacing such expressions with the explicit null. Firstly, it will improve the readability of the code, but it can also improve performance when the expression requires some calculations or if the code is a part of a heavily used API.

The example below demonstrates the simplest case where it is obvious that input will be always null inside the if (input == null) condition. However, in real-life situations, the always-null expression can be separated from the related check by multiple lines of code and would not be that easy to spot.

#nullable disable void Parse(string input) { if (input == null) Console.WriteLine(input); }
#nullable disable void Parse(string input) { if (input == null) Console.WriteLine((string)null); }
Last modified: 27 August 2024