使用自动实现的属性 (IDE0032)

属性
规则 ID IDE0032
标题 使用自动实现的属性
类别 Style
Subcategory 语言规则(表达式级首选项)
适用的语言 C# 和 Visual Basic
引入的版本 Visual Studio 2017
选项 dotnet_style_prefer_auto_properties

概述

此样式规则涉及使用自动实现的属性,而不是专用支持字段的属性。

选项

选项指定你希望规则强制实施的行为。 若要了解如何配置选项,请参阅选项格式

dotnet_style_prefer_auto_properties

属性 说明
选项名称 dotnet_style_prefer_auto_properties
选项值 true 首选自动实现的属性
false 首选包含专用支持字段的属性
默认选项值 true
// dotnet_style_prefer_auto_properties = true
public int Age { get; }

// dotnet_style_prefer_auto_properties = false
private int age;

public int Age
{
    get
    {
        return age;
    }
}
' dotnet_style_prefer_auto_properties = true
Public ReadOnly Property Age As Integer

' dotnet_style_prefer_auto_properties = false
Private _age As Integer

Public ReadOnly Property Age As Integer
    Get
        return _age
    End Get
End Property

抑制警告

如果只想抑制单个冲突,请将预处理器指令添加到源文件以禁用该规则,然后重新启用该规则。

#pragma warning disable IDE0032
// The code that's violating the rule is on this line.
#pragma warning restore IDE0032

若要对文件、文件夹或项目禁用该规则,请在配置文件中将其严重性设置为 none

[*.{cs,vb}]
dotnet_diagnostic.IDE0032.severity = none

若要禁用所有代码样式规则,请在配置文件中将类别 Style 的严重性设置为 none

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

有关详细信息,请参阅如何禁止显示代码分析警告

另请参阅