括号首选项(IDE0047 和 IDE0048)

本文介绍了两个相关的规则:IDE0047IDE0048

属性
规则 ID IDE0047
标题 删除不必要的括号
类别 Style
Subcategory 语言规则(括号首选项)
适用的语言 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
属性
规则 ID IDE0048
标题 为清楚起见,请添加括号
类别 Style
Subcategory 语言规则(括号首选项)
适用的语言 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

有关详细信息,请参阅如何禁止显示代码分析警告

另请参阅