Search code examples
c#visual-studio-2019roslyn-code-analysisobject-initializers

Configure code cleanup profile to delete object initializer constructor parentheses


How can I configure my code cleanup profile in Visual Studio 2019 to change this piece of code

new List<Alias>() { key }

to this one

new List<Alias> { key }

when I am running code cleanup?


Solution

  • There is no option in the Code Style section as you can see in the editor config official document

    The only option is for dotnet_style_collection_initializer

    // dotnet_style_collection_initializer = true
    var list = new List<int> { 1, 2, 3 };
    
    // dotnet_style_collection_initializer = false
    var list = new List<int>();
    list.Add(1);
    list.Add(2);
    list.Add(3);
    

    This will remove the () if it refactors from non-collection-initializers. There is not option to refactor if the initialization occurs with the collection initalizer with the () inline.