Skip to content

BRO1510: Accessors should all be single-line or all multi-line

When every accessor of a property, indexer or event has a block body, they are laid out the same way: each on one line, or each over several lines. Replaces StyleCop SA1504.

Example

Before

public class Temperature
{
    private double celsius;

    public double Celsius
    {
        get { return this.celsius; }

        set
        {
            this.celsius = value;
        }
    }
}

After

public class Temperature
{
    private double celsius;

    public double Celsius
    {
        get { return this.celsius; }

        set { this.celsius = value; }
    }
}

Why

A property whose accessors are written differently looks like the two halves were changed separately, and the eye has to switch styles in the middle of a declaration.

How the fix works

Like StyleCop's fix, it goes the short way when it can: if every multi-line accessor holds at most one statement on a single line, those accessors are put on one line (set { this.celsius = value; }). Otherwise the single-line accessors are expanded, with their braces on their own lines (or kept on the accessor's line when csharp_new_line_before_open_brace doesn't include accessors) and the statements one level deeper. A body with a comment in it is never collapsed. The blank line BRO1505 wants between two block-bodied accessors is added at the same time, so one dotnet format run is enough.

Not reported

  • An accessor without a block body, like StyleCop: get => this.x; or get; next to a multi-line set { ... } is fine.
  • Accessor lists on one line ({ get { return x; } set { x = value; } }): that's BRO1509's.
  • An accessor that shares its line with the previous one, or a comment in a gap the fix rewrites.

An accessor counts as single-line when its keyword (or first modifier) and its body are on one line; an attribute on its own line above it doesn't count.

Compared with StyleCop

Same positions as StyleCop, except accessors with an attribute on its own line: StyleCop counts the attribute line, so it reports [DebuggerStepThrough] + get { return x; } next to set { x = value; } (both on one line) and misses the same get next to a multi-line set (#3434). StyleBro does the opposite. StyleCop's fix offers "single line" and "multiple lines" separately, and under dotnet format one of them is applied to the whole project, whichever the first diagnostic offers; it also deletes comments in a body it collapses. StyleBro decides per property and keeps comments.