แก้ไข

แชร์ผ่าน


Null check can be simplified (IDE0029, IDE0030, and IDE0270)

This article describes three related rules, IDE0029, IDE0030, and IDE0270.

Property Value
Rule ID IDE0029
Title Null check can be simplified (ternary conditional check)
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# and Visual Basic
Options dotnet_style_coalesce_expression
Property Value
Rule ID IDE0030
Title Null check can be simplified (nullable ternary conditional check)
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# and Visual Basic
Options dotnet_style_coalesce_expression
Property Value
Rule ID IDE0270
Title Null check can be simplified (if null check)
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# and Visual Basic
Options dotnet_style_coalesce_expression

Overview

Rules IDE0029 and IDE0030 concern the use of null-coalescing expressions, for example, x ?? y, versus ternary conditional expressions with null checks, for example, x != null ? x : y. The rules differ with respect to the nullability of the expressions:

  • IDE0029: Used when non-nullable expressions are involved. For example, this rule could recommend x ?? y instead of x != null ? x : y when x and y are non-nullable reference types.
  • IDE0030: Used when nullable expressions are involved. For example, this rule could recommend x ?? y instead of x != null ? x : y when x and y are nullable value types or nullable reference types.

Rule IDE0270 flags the use of a null check (== null or is null) instead of the null-coalescing operator (??).

Options

Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.

dotnet_style_coalesce_expression

Property Value Description
Option name dotnet_style_coalesce_expression
Option values true Prefer null-coalescing expressions.
false Disables the rule.
Default option value true

Examples

IDE0029 and IDE0030

// Code with violation.
var v = x != null ? x : y; // or
var v = x == null ? y : x;

// Fixed code.
var v = x ?? y;
' Code with violation.
Dim v = If(x Is Nothing, y, x) ' or
Dim v = If(x IsNot Nothing, x, y)

' Fixed code.
Dim v = If(x, y)

IDE0270

// Code with violation.
class C
{
    void M()
    {
        var item = FindItem() as C;
        if (item == null)
            throw new System.InvalidOperationException();
    }

    object? FindItem() => null;
}

// Fixed code (dotnet_style_coalesce_expression = true).
class C
{
    void M()
    {
        var item = FindItem() as C ?? throw new System.InvalidOperationException();
    }

    object? FindItem() => null;
}
' Code with violation.
Public Class C
    Sub M()
        Dim item = TryCast(FindItem(), C)
        If item Is Nothing Then
            item = New C()
        End If
    End Sub

    Function FindItem() As Object
        Return Nothing
    End Function
End Class

' Fixed code (dotnet_style_coalesce_expression = true).
Public Class C
    Sub M()
        Dim item = If(TryCast(FindItem(), C), New C())
    End Sub

    Function FindItem() As Object
        Return Nothing
    End Function
End Class

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable IDE0029 // Or IDE0030 or IDE0270
// The code that's violating the rule is on this line.
#pragma warning restore IDE0029 // Or IDE0030 or IDE0270

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0029.severity = none
dotnet_diagnostic.IDE0030.severity = none
dotnet_diagnostic.IDE0270.severity = none

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also