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: Variable can be declared as non-nullable

Last modified: 25 September 2024

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.

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