括號偏好(IDE0047和IDE0048)
本文說明兩個相關規則,IDE0047
和 IDE0048
。
財產 | 價值 |
---|---|
規則標識碼 | 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_relational_binary_operators
- 其他二元運算子 - dotnet_style_parentheses_in_other_binary_operators
- 其他運算子 - dotnet_style_parentheses_in_other_operators
如需設定選項的相關資訊,請參閱 選項格式。
算術運算符號的括號樣式設定 (dotnet_style_parentheses_in_arithmetic_binary_operators)
財產 | 價值 | 描述 |
---|---|---|
選項名稱 | dotnet_運算法的括號風格_算術二元運算子 | |
選項值 | 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_樣式_關係_二元運算子的括號使用規範
財產 | 價值 | 描述 |
---|---|---|
選項名稱 | dotnet_style_parentheses_in_relational_binary_operators | |
選項值 | always_for_clarity |
偏好括弧來釐清關係運算符優先順序 |
never_if_unnecessary |
當關係運算符優先順序明顯時,偏好不要有括弧 | |
預設選項值 | always_for_clarity |
關係型二元運算符包括:>
、<
、<=
、>=
、is
、as
、==
和 !=
。
// 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_其他二元運算符的括號
財產 | 價值 | 描述 |
---|---|---|
選項名稱 | dotnet_其他二元運算子中的括號風格 | |
選項值 | 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 風格括號
財產 | 價值 | 描述 |
---|---|---|
選項名稱 | 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
如需詳細資訊,請參閱 如何在隱藏程式代碼分析警告。