简化属性访问器(IDE0360)

资产 价值
规则 ID IDE0360
标题 简化属性访问器
类别 Style
子类别 语言规则(表达式级首选项)
适用的语言 C# 13+
Options csharp_style_prefer_simple_property_accessors

概述

此规则标记出属性访问器在直接访问field关键字(适用于C# 13+)时可以简化的位置。 当属性访问器仅返回 field 或赋值 field时,它可以简化为简单的自动访问器。

选项

选项指定希望规则强制实施的行为。 有关配置选项的信息,请参阅 选项格式

csharp_style_prefer_simple_property_accessors(C# 样式:倾向简单属性访问器)

资产 价值 Description
选项名称 csharp_style_prefer_simple_property_accessors
选项值 true 首选简化的属性访问器
false 禁用规则
默认选项值 true

Example

// Code with violations.
public int Prop
{
    get { return field; }
    set { field = (value > 0) ?  value : throw new ArgumentException(); }
}

// Fixed code.
public int Prop { get; set; }

禁止显示警告

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

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

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

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

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

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

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