TypeName | SA1520UseBracesConsistently |
CheckId | SA1520 |
Category | Layout Rules |
📝 This rule is new for StyleCop Analyzers, and was not present in StyleCop Classic.
The opening and closing braces of a chained if
/else if
/else
construct were included for some clauses, but
omitted for others.
A violation of this rule occurs when the opening and closing braces for a chained statement have been included
for some clauses but omitted for others. In C#, some types of statements may optionally include braces. For
example, an if
-statement may be written with inconsistent braces:
if (true)
return this.value;
else
{
return that.value.
}
Although this is legal in C#, StyleCop requires the braces to be present for all clauses of a chained if
/else if
/else
construct when braces are included for any clause, to increase the readability and maintainability of the
code.
To fix a violation of this rule, the violating statement will be converted to a block statement.
if (true)
#pragma warning disable SA1520 // Use braces consistently
return this.value;
#pragma warning restore SA1520 // Use braces consistently
else
{
return that.value.
}