BRO1141: Object creation with an initializer: parentheses in one style¶
An object creation with an initializer and no arguments is written without the empty parentheses
(new List<int> { 1 }, the default) or with them (new List<int>() { 1 }). Not a StyleCop rule (Roslynator RCS1050).
Example¶
Before¶
using System.Collections.Generic;
public class Settings
{
public string Name { get; set; } = "";
public List<int> Ports = new List<int>() { 80, 443 };
public static Settings Default() => new Settings() { Name = "default" };
}
After¶
using System.Collections.Generic;
public class Settings
{
public string Name { get; set; } = "";
public List<int> Ports = new List<int> { 80, 443 };
public static Settings Default() => new Settings { Name = "default" };
}
Why¶
The parentheses say nothing when there are no arguments; the initializer shows that an object is built. One way to write it keeps creations consistent.
How the fix works¶
omit removes the parentheses and any spaces before them; include inserts () right after the type.
Configuration¶
[*.cs]
# omit (default): new List<int> { 1 }; include: new List<int>() { 1 }
stylebro_object_creation_parentheses = omit
Not reported¶
- Creations with arguments (
new List<int>(4) { 1 }) or without an initializer (new List<int>()). - Target-typed
new() { ... }: it needs its parentheses. - A comment between the type and the parentheses.
Compared with StyleCop¶
StyleCop has no such rule. stylebro-migrate turns it off (see
Rules beyond StyleCop).