ReSharper 2024.2 Help

Code inspection: Variable can be declared as non-nullable

If nullable reference types (NRT) are enabled, this inspection reports redundant nullable reference types' annotations (?) on variables whose value is determined to never be null based on the control flow analysis.

In the example below, the value of the myClass variable will never be null because it is assigned with a new instance of MyClass.

ReSharper suggests removing the ? annotation on the type of the variable to improve readability and to avoid unnecessary null checks for usages of the variable.

#nullable enable MyClass? myClass = new MyClass();
#nullable enable MyClass? myClass = new MyClass();

For more information about NRT and how ReSharper supports them, watch this webinar recording:

Last modified: 25 September 2024