An old trick for new dogs: collection initialization with an extension method in C# 6

Akos Nagy
Mar 6, 2019

During one of my courses, I was talking about the new language features of C# 3 to introduce the students LInQ. I was discussing collection initializers and extensions methods, when I made an off-hand remark about C# 6 allowing for the Add() method used for the collection initialization to be an extension method as well. Unfortunately, I didn't have the time to explain in detail, and I could see that not everyone was getting why this was good. So here's a blog post explaining why is this good :)

The basic idea goes like this: you have a Person class and want to fill a list of people with collection initializers. Currently, you can do it like this:

public class Person
{
  public int Age { get; set; }
  public string Name { get; set; }
}

//...
var people = new List<Person> {
                 new Person { Age = 19, Name = "Akos" },
                 new Person { Age = 26, Name = "Akos2" },
             };

You can make this code a little more compact if you write a constructor for the Person class, but that's it.

But with C# 6, the Add() method used by the collection initializer can be an extension method as well. So you can create an extension method like this:

public static class ListOfPersonExtensions
{
  public static void Add(this ICollection<Person> people, int age, string name) =>
                     people.Add(new Person { Age = age, Name = name });
}

And in C# 6, this also gets picked up by the compiler when checking for compatibility for this feature. So after including the proper using for this class, you can now simply write:

var people = new List<Person> { {19, "Akos"}, {26, "Akos2"} };

I think it's cool. Of course there is the question of readability. I guess now you don't know which values go where in the objects. Not that you knew if you used constructors, but with properties, this was much more straightforward. But unless you never use constructors, this solution is just as good :)

Akos Nagy
Posted in C# Teaching