Parentheses preferences (IDE0047 and IDE0048)

This article describes two related rules, IDE0047 and IDE0048.

Property Value
Rule ID IDE0047
Title Remove unnecessary parentheses
Category Style
Subcategory Language rules (parentheses preferences)
Applicable languages C# and Visual Basic
Introduced version Visual Studio 2017
Options 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
Property Value
Rule ID IDE0048
Title Add parentheses for clarity
Category Style
Subcategory Language rules (parentheses preferences)
Applicable languages C# and Visual Basic
Introduced version Visual Studio 2017
Options 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

Overview

The style rules in this section concern parentheses preferences, including the use of parentheses to clarify precedence for arithmetic, relational, and other binary operators.

Options

This rule has associated options to specify preferences based on the type of operator:

For information about configuring options, see Option format.

dotnet_style_parentheses_in_arithmetic_binary_operators

Property Value Description
Option name dotnet_style_parentheses_in_arithmetic_binary_operators
Option values always_for_clarity Prefer parentheses to clarify arithmetic operator precedence
never_if_unnecessary Prefer no parentheses when arithmetic operator precedence is obvious
Default option value always_for_clarity

The arithmetic binary operators are: *, /, %, +, -, <<, >>, &, ^, and |.

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

Property Value Description
Option name dotnet_style_parentheses_in_relational_binary_operators
Option values always_for_clarity Prefer parentheses to clarify relational operator precedence
never_if_unnecessary Prefer to not have parentheses when relational operator precedence is obvious
Default option value always_for_clarity

The relational binary operators are: >, <, <=, >=, is, as, ==, and !=.

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

Property Value Description
Option name dotnet_style_parentheses_in_other_binary_operators
Option values always_for_clarity Prefer parentheses to clarify other binary operator precedence
never_if_unnecessary Prefer to not have parentheses when other binary operator precedence is obvious
Default option value always_for_clarity

The other binary operators are: &&, ||, and ??.

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

Property Value Description
Option name dotnet_style_parentheses_in_other_operators
Option values always_for_clarity Prefer parentheses to clarify other operator precedence
never_if_unnecessary Prefer to not have parentheses when other operator precedence is obvious
Default option value never_if_unnecessary

This option applies to operators other than the following:

*, /, %, +, -, <<, >>, &, ^, | >, <, <=, >=, 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

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

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

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

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

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

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

For more information, see How to suppress code analysis warnings.

See also