Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 747
Aleksei Zaitsev Avatar asked May 20 '26 17:05

Aleksei Zaitsev


1 Answers

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.

like image 139
Athanasios Kataras Avatar answered May 23 '26 05:05

Athanasios Kataras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!