แก้ไข

แชร์ผ่าน


Simplify property accessor (IDE0360)

Property Value
Rule ID IDE0360
Title Simplify property accessor
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# 13+
Options csharp_style_prefer_simple_property_accessors

Overview

This rule flags places where a property accessor that directly accesses the field keyword (C# 13+) can be simplified. When a property accessor only returns field or assigns a value to field, it can be simplified to a simple auto-accessor.

Options

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

csharp_style_prefer_simple_property_accessors

Property Value Description
Option name csharp_style_prefer_simple_property_accessors
Option values true Prefer simplified property accessors
false Disables the rule
Default option value 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; }

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 IDE0360
// The code that's violating the rule is on this line.
#pragma warning restore IDE0360

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

[*.{cs,vb}]
dotnet_diagnostic.IDE0360.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.