BRO1510: Accessors should all be single-line or all multi-line¶
When every accessor of a property, indexer or event has a body, a block or an expression (get => this.x;), 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¶
The fix 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.
An expression-bodied accessor on one line next to a one-statement block keeps its expression body, and the block joins it on one line:
public double Celsius
{
get => this.celsius;
set { this.celsius = value; }
}
Next to a block that can't go on one line, the expression-bodied accessor becomes a block: get => this.celsius;
turns into get, {, return this.celsius;, }, and set => this.celsius = value; (or init, add, remove)
into a block holding this.celsius = value;:
public double Celsius
{
get
{
return this.celsius;
}
set
{
this.celsius = value;
this.Changed?.Invoke(this, EventArgs.Empty);
}
}
An expression body that spans several lines counts as a multi-line accessor and stays as written; a one-line accessor next to it is expanded.
Not reported¶
- An accessor without a body:
get;next to a multi-lineset { ... }is fine. - Accessors that are all on one line each, whatever their bodies:
get => this.x;aboveset { this.x = value; }. - An expression-bodied accessor that would become a block while expression bodies are preferred:
csharp_style_expression_bodied_accessorsistrueorwhen_on_single_line, or IDE0027 is on and that option isn't set. IDE0027's fix would turn the block back into an expression body. - An expression-bodied accessor with a comment between its keyword and its
;, when it would become a block. - 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.