Null check can be simplified (IDE0029, IDE0030, and IDE0270)
Bài viết
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
C#
// Code with violation.var v = x != null ? x : y; // orvar 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
C#
// Code with violation.classC
{
voidM()
{
var item = FindItem() as C;
if (item == null)
thrownew System.InvalidOperationException();
}
object? FindItem() => null;
}
// Fixed code (dotnet_style_coalesce_expression = true).classC
{
voidM()
{
var item = FindItem() as C ?? thrownew 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.
C#
#pragmawarning disable IDE0029 // Or IDE0030 or IDE0270// The code that's violating the rule is on this line.#pragmawarning 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.
Bạn có thể tìm thấy nguồn cho nội dung này trên GitHub, nơi bạn cũng có thể tạo và xem lại các vấn đề và yêu cầu kéo. Để biết thêm thông tin, hãy xem hướng dẫn dành cho người đóng góp của chúng tôi.
Ý kiến phản hồi về .NET
.NET là một dự án nguồn mở. Chọn liên kết để cung cấp ý kiến phản hồi:
Tham gia chuỗi buổi gặp gỡ để xây dựng các giải pháp AI có thể mở rộng dựa trên các trường hợp sử dụng trong thế giới thực với các nhà phát triển và chuyên gia đồng nghiệp.