Use collection initializers

Update and initialize items in old collection declarations

Initializing members of a collection or list

For many years the de facto way to create List objects and other collection types was by first declaring a variable, and then populating it with several calls to an Add (or similarly named) method. While this works perfectly fine, the syntax is a bit outdated. So you may want to update it. The more modern way to declare and initialize a collection is to both declare the list or collection while instantiating several individual member objects.

To make this change manually requires both changing the syntax of the items added to the collection, then moving those items into a constructor. This is a lot of small changes, specifically deleting parenthesis, curly brackets and other tokens. What seems like a small update can result in a lot of syntax errors. Fortunately, Rider reduces this work to two keystrokes and zero syntax errors. When Rider sees the original declaration style, it displays a green indicator on the new keyword where the list or collection is instantiated. Place the caret on the green indicator under the new keyword, and press Alt+Enter to see what options are available. In this case, Rider suggests changing to a collection initializer. Choosing it changes the code so that it uses property initializers for each object in the List using a modern syntax.

Under the hood, C# still makes calls to Add (or similar), but your code becomes much more readable!

Related Resources

Rename refactoring
Would a variable by any other name read as clearly?
Extract method refactoring
Split and Organize code into DRY, discrete units.
Inline method refactoring
Replace usages of a function with its implementation.