使用複合指派 (IDE0054 和 IDE0074)

本文說明兩個相關規則 IDE0054IDE0074

屬性
規則識別碼 IDE0054
標題 使用複合指派
類別 樣式
子類別 語言規則 (運算式層級喜好設定)
適用語言 C# 和 Visual Basic
選項 dotnet_style_prefer_compound_assignment
屬性
規則識別碼 IDE0074
標題 使用聯合複合指派
類別 樣式
子類別 語言規則 (運算式層級喜好設定)
適用語言 C# 和 Visual Basic
選項 dotnet_style_prefer_compound_assignment

概觀

這些規則是關於使用複合指派。 報告 IDE0074 表示聯合複合指派,報告 IDE0054 則表示其他複合指派。

選項

選項值會指定是否需要複合指派。

如需設定選項的相關資訊,請參閱選項格式

dotnet_style_prefer_compound_assignment

屬性 描述
選項名稱 dotnet_style_prefer_compound_assignment
選項值 true 偏好複合指派運算式
false 不偏好複合指派運算式
預設選項值 true
// dotnet_style_prefer_compound_assignment = true
x += 5;

// dotnet_style_prefer_compound_assignment = false
x = x + 5;
' dotnet_style_prefer_compound_assignment = true
x += 5

' dotnet_style_prefer_compound_assignment = false
x = x + 5

隱藏警告

若您只想隱藏單一違規,請將前置處理指示詞新增至來源檔案以停用規則,然後重新啟用規則。

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

若要停用檔案、資料夾或專案的規則,請在組態檔中將其嚴重性設定為 none

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

若要停用所有程式碼樣式規則,請在組態檔中將類別 Style 的嚴重性設定為 none

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

如需詳細資訊,請參閱如何隱藏程式碼分析警告

另請參閱