括号首选项(IDE0047 和 IDE0048)

本文介绍两个相关规则,IDE0047IDE0048

财产 价值
规则 ID 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
财产 价值
规则 ID 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

此选项适用于除以下 以外的运算符

*/%+-<<>>&^|><<=>=isas==!=&&||??

// 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

有关详细信息,请参阅 如何取消代码分析警告

另请参阅