Skip to content

BRO1113: Regions should not be placed inside code elements

#region isn't used inside a { } block of a method, accessor, lambda or other code. Replaces StyleCop SA1123. Regions between members and types, and in expression bodies (like StyleCop, which reports those as SA1124), are BRO1112.

Example

Before

public class Importer
{
    public int Import(string[] lines)
    {
        var count = 0;
        #region Parse lines
        foreach (var line in lines)
        {
            count += line.Length > 0 ? 1 : 0;
        }
        #endregion
        return count;
    }
}

After

public class Importer
{
    public int Import(string[] lines)
    {
        var count = 0;
        foreach (var line in lines)
        {
            count += line.Length > 0 ? 1 : 0;
        }
        return count;
    }
}

Why

A region inside a member hides part of its logic. If a part of a method deserves a name, extract it into a method with that name.

How the fix works

The #region and #endregion lines are removed, with the same blank-line cleanup as BRO1112.

Not reported

A region in code that an #if turns off, like BRO1112.

Compared with StyleCop

Same results as StyleCop, with the same fixed output. StyleCop turns SA1123 on by default, and so does StyleBro's preset (unlike BRO1112, which the preset leaves off although StyleCop turns SA1124 on too).