BRO1527: Auto-accessors should be on one line¶
A property or indexer whose accessors are all auto-accessors (get;, set;, init;, also with a modifier such as
private set;) goes on one line: public int Simple { get; set; }. Not a StyleCop rule: from Roslynator's
RCS0042.
Example¶
Before¶
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
private set;
} = 18;
}
After¶
public class Person
{
public string Name { get; set; }
public int Age { get; private set; } = 18;
}
Why¶
The one-line form is how auto-properties are written almost everywhere; spread over five lines they look like a
property with logic. The SDK's IDE0360 (get { return field; } -> get;, in stylebro-migrate init --modernize)
leaves the accessors on their old lines; this rule finishes the job.
How the fix works¶
The whitespace from the declaration's name to the {, between the accessors and before the } becomes one space each.
An initializer (= 18;) stays after the }. While this rule is on, BRO1505 (and
BRO1001's sort, which adds the blank lines BRO1505 wants) already treat a property this fix will put on one
line as a one-line property, so two such properties may sit together whichever fix dotnet format runs first. When
BRO1505 is off, the fix adds the blank line BRO1519 wants after the multi-line property's }, for the same
reason.
IDE0360's fix runs in dotnet format's code-style pass, before the analyzer pass with this rule, so a property it
shortens is put on one line in the same run, and IDE0360 doesn't touch the result. They don't fight.
Not reported¶
- An accessor with a body (
get { ... }), an expression body (get => x;) or an attribute ([Obsolete] get;). - A comment or directive anywhere from the declaration's name to the
}, or a line break inside an accessor. - An initializer that doesn't start on the
}line. - A declaration whose modifiers, type and name span several lines (a generic type split over lines, as CSharpier
writes it): the SDK's formatter (IDE0055) puts the
{on a line of its own then, and the two would undo each other. csharp_preserve_single_line_blocks = falsein.editorconfig: the SDK's formatter (IDE0055) would expand the list again. The preset sets it totrue.
A long line isn't split or avoided: the one-line form is as long as the declaration plus the accessors.
Compared with StyleCop¶
StyleCop has no such rule (SA1502 allows { get; set; } on one line but doesn't ask for it). stylebro-migrate turns
it off (see Rules beyond StyleCop).