| 房產 | 價值觀 |
|---|---|
| 規則識別碼 | IDE0360 |
| 標題 | 簡化屬性存取子 |
| 類別 | Style |
| 子類別 | 語言規則(表達層級偏好) |
| 適用語言 | C# 13+ |
| 選項 | csharp_style_prefer_simple_property_accessors |
概觀
此規則會標示可簡化直接存取 field 關鍵字 (C# 13+) 的屬性存取子的位置。 當屬性存取器只傳回 field 或指派值 field給 時,它可以簡化為簡單的自動存取器。
選項
選項會指定您希望規則強制執行的行為。 如需設定選項的相關資訊,請參閱 選項格式。
csharp_樣式_偏好簡單屬性存取器
| 房產 | 價值觀 | 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
若要停用檔案、資料夾或項目的規則,請在組態檔
[*.{cs,vb}]
dotnet_diagnostic.IDE0360.severity = none
若要停用所有程式碼樣式規則,請將類別 Style 的嚴重性設定為 組態檔中的 none。
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
如需詳細資訊,請參閱 如何隱藏程式碼分析警告。