括弧喜好設定 (IDE0047 和 IDE0048)

本文描述兩個相關規則 IDE0047IDE0048

屬性
規則識別碼 IDE0047
標題 移除不必要的括弧
類別 樣式
子類別 語言規則 (括弧喜好設定)
適用語言 C# 和 Visual Basic
引進的版本 Visual Studio 2017
選項 dotnet_style_parentheses_in_arithmetic_binary_operators
dotnet_style_parentheses_in_relational_binary_operators
dotnet_style_parentheses_in_other_binary_operators
dotnet_style_parentheses_in_other_operators
屬性
規則識別碼 IDE0048
標題 新增括弧以明確表示
類別 樣式
子類別 語言規則 (括弧喜好設定)
適用語言 C# 和 Visual Basic
引進的版本 Visual Studio 2017
選項 dotnet_style_parentheses_in_arithmetic_binary_operators
dotnet_style_parentheses_in_relational_binary_operators
dotnet_style_parentheses_in_other_binary_operators
dotnet_style_parentheses_in_other_operators

概觀

本節中的樣式規則與括弧喜好設定有關,包括使用括弧來說明算數、關係及其他二元運算子的優先順序。

選項

此規則具有根據運算子類型指定喜好設定的相關選項:

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

dotnet_style_parentheses_in_arithmetic_binary_operators

屬性 描述
選項名稱 dotnet_style_parentheses_in_arithmetic_binary_operators
選項值 always_for_clarity 建議使用括弧來說明算術運算子優先順序
never_if_unnecessary 當算術運算子優先順序很明顯時建議不使用括弧
預設選項值 always_for_clarity

算術二元運算子包括:*/%+-<<>>&^|

// dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
var v = a + (b * c);

// dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary
var v = a + b * c;
' dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
Dim v = a + (b * c)

' dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary
Dim v = a + b * c

dotnet_style_parentheses_in_relational_binary_operators

屬性 描述
選項名稱 dotnet_style_parentheses_in_relational_binary_operators
選項值 always_for_clarity 建議使用括弧來說明關係運算子優先順序
never_if_unnecessary 當關係運算子優先順序很明顯時建議不使用括弧
預設選項值 always_for_clarity

關係二元運算子包括:><<=>=isas==!=

// dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
var v = (a < b) == (c > d);

// dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary
var v = a < b == c > d;
' dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
Dim v = (a < b) = (c > d)

' dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary
Dim v = a < b = c > d

dotnet_style_parentheses_in_other_binary_operators

屬性 描述
選項名稱 dotnet_style_parentheses_in_other_binary_operators
選項值 always_for_clarity 建議使用括弧來說明其他二元運算子優先順序
never_if_unnecessary 當其他二元運算子優先順序很明顯時建議不使用括弧
預設選項值 always_for_clarity

其他二元運算子包括:&&||??

// dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
var v = a || (b && c);

// dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary
var v = a || b && c;
' dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
Dim v = a OrElse (b AndAlso c)

' dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary
Dim v = a OrElse b AndAlso c

dotnet_style_parentheses_in_other_operators

屬性 描述
選項名稱 dotnet_style_parentheses_in_other_operators
選項值 always_for_clarity 建議使用括弧來說明其他運算子優先順序
never_if_unnecessary 當其他運算子優先順序很明顯時建議不使用括弧
預設選項值 never_if_unnecessary

此選項適用於下列運算子「以外」的運算子:

*, /, %, +, -, <<, >>, &, ^, |>, <, <=, >=, is, as, ==, !=&&, ||, ??

// dotnet_style_parentheses_in_other_operators = always_for_clarity
var v = (a.b).Length;

// dotnet_style_parentheses_in_other_operators = never_if_unnecessary
var v = a.b.Length;
' dotnet_style_parentheses_in_other_operators = always_for_clarity
Dim v = (a.b).Length

' dotnet_style_parentheses_in_other_operators = never_if_unnecessary
Dim v = a.b.Length

隱藏警告

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

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

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

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

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

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

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

另請參閱