BRO1112: Do not use regions¶
#region isn't used between members or types, or in an expression body (between the arms of a switch expression
after =>). Replaces StyleCop SA1124. Regions inside a { } block are BRO1113.
Example¶
Before¶
public class Account
{
#region Fields
private decimal balance;
#endregion
#region Methods
public void Deposit(decimal amount)
{
balance += amount;
}
#endregion
}
After¶
public class Account
{
private decimal balance;
public void Deposit(decimal amount)
{
balance += amount;
}
}
Why¶
Regions hide code instead of organizing it: a class that needs them to be readable is usually doing too much, and collapsed regions make reviewers miss what's inside. Member ordering (BRO1001) already groups members by kind.
StyleCop turns SA1124 on by default, but StyleBro's preset leaves BRO1112 off: removing every region in an existing codebase is a large one-time change, so it's opt-in. Turn it on to remove regions:
[*.cs]
dotnet_diagnostic.BRO1112.severity = warning
How the fix works¶
The #region and #endregion lines are removed. A blank line next to them goes too when it would otherwise double
another blank line, follow an opening brace or precede a closing brace. Directives inside the region (#if) stay.
Removing regions lets BRO1001 sort across them: member ordering sorts within each region only. When
BRO1001 is on, the fix sorts the members right after removing the regions, so one dotnet format run is enough. It
sorts the way BRO1001's own fix does, including the blank lines BRO1505 and
BRO1509 want made before sorting, so the result doesn't depend on which fix runs first.
Not reported¶
A region in code that an #if turns off: there the code around it is plain text, so neither the rule (BRO1112 or
BRO1113) nor the blank-line cleanup can be decided. In a multi-targeted project the region is reported
and removed through the target frameworks where the code is active.
Compared with StyleCop¶
Same results as StyleCop, with the same fixed output.