All rules
IDE0240Unnecessary code rules (expression-level preferences)

Nullable directive is redundant

Nullable directive is redundant

Microsoft docs

Description

This rule flags places where a #nullable directive is redundant. For example:

  • #nullable enable following another #nullable enable with no disablement in between.
  • #nullable enable in a project where nullable context is already enabled and hasn't been disabled.
  • #nullable restore following another #nullable restore with no nullability context change in between.
  • #nullable restore in a place that already matches the project context.

Example

// Code with violations (in a project with <Nullable>enable</Nullable>).
#nullable enable
Console.WriteLine("Hello world.");

// Fixed code.
Console.WriteLine("Hello world.");
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0