Collection Initializers gone from C# 3.0

time to read 3 min | 562 words

David Hayden points out that the syntax for Collection Initializers for C# 3.0 has changed, and in my eyes, this render the entire idea of collection initializers useless.

Before:

List<Customer> listOfCustomers = new List<Customer> {     
{ Id = 1, Name="Dave", City="Sarasota" },
{ Id
= 2, Name="John", City="Tampa" },
{ Id
= 3, Name="Abe", City="Miami" } };

After:

List<Customer> listOfCustomers = new List<Customer> {
new Customer { Id = 1, Name="Dave", City="Sarasota" },
new Customer { Id = 2, Name="John", City="Tampa" },
new Customer { Id = 3, Name="Abe", City="Miami" } };

Today, using arrays:

Customer[] listOfCustomers =
        new Customer[] {
            new Customer { 1, "Dave""Sarasota" },
            new Customer { 2, "John""Tampa" },
            new Customer { 3, "Abe""Miami" }
        };

One of the major good things that collection initializers gives is the ability to define a static set of data in readable and easy to modify format. In languages that has a dictionary initializers, this is even more useful. The main idea is the reduce the visual clutter. I just don't see it happening here.