All rules
IDE0241Unnecessary code rules (expression-level preferences)

Nullable directive is unnecessary

Nullable directive is unnecessary

Microsoft docs

Description

This rule flags places where a #nullable directive is unnecessary. The directive is unnecessary where there are no reference types that would be impacted by the nullable context scope change. For example:

  • #nullable enable in a project where nullable context is already enabled.
  • #nullable disable in a project where nullable context is already disabled.
  • #nullable disable for an enumeration.
  • #nullable disable or #nullable restore at the end of a file that includes #nullable enable in a project where nullable context is disabled.

Example

// Code with violations (in a project with <Nullable>enable</Nullable>).
#nullable disable
enum Place
{
    First,
    Second
}

// Fixed code.
enum Place
{
    First,
    Second
}
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0