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, 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.

The preset turns BRO1113 on (unlike BRO1112, which it leaves off).

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.

A region the repository's docs include as a MarkdownSnippets snippet (#region ShouldBeObjects and a snippet: ShouldBeObjects line in a markdown file, with an mdsnippets.json at or above the project): without it the docs lose the snippet and MarkdownSnippets' build step fails with "Missing snippet". The package's build targets read the snippet names from the repository's markdown files (.md and .mdx; bin, obj and node_modules left out): snippet: Name in any case, also include: Name, which takes a snippet too.