Collection initializers and Add method with more than one parameter

I didn’t know this about C# until today. This code compiles:

using System;
using System.Collections.Generic;
class List : List<Tuple<string, int, bool>>
    public void Add(string s, int i, bool b)
        Add(Tuple.Create(s, i, b));
    static void Main(string[] args)
        var list = new List
            { "zero", 0, true },
            { "one", 1, false }
        foreach (var item in list)

and outputs:

(zero, 0, True)

(one, 1, False)

It turns out, the way collection initializers work they pattern match to expect the Add method on the list object, and it can have more than one or two parameters. This is extremely handy for writing declarative, data-driven unit-tests where you only specify the data for your scenario and the imperative control-flow of the test is abstracted away into a helper method. This allows you to shape the relevant data in a way convenient to be able to clearly read the test scenario.

P.S. For a second, I admit to having a thought “I bet even Jon Skeet didn’t know about this one”, but then of course I opened C# in Depth 2nd Edition, page 216 and was once again in awe of Jon’s superpowers. Sorry for ever having the slightest of doubt, Jon, won’t happen again!

Comments (7)

  1. James Manning says:

    WRT data-driven unit tests, some links for others that may be interested.  Most of the frameworks seem to support both 'inline' data (declared within the source) and having it pulled from an external source (excel, database, etc)

    – Parameterized unit tests in NUnit @

    – Theory tests in xunit @

    – data-driven in mstest @…/hh694602.aspx

    If you can use your test framework's support and your test runner has support for it as well, it's a little nicer since they can be treated as N different tests in the results (giving you the additional details of which passed and which failed) instead of just "one of them failed" you get when doing it 'manually' in a single test 🙂

  2. James Manning says:

    sorry, should have included this in the last comment 🙁

    One usage scenario I see some people miss out on using is that you can use collection initializers inside property initializers even if you're not declaring a new instance.

    In many scenarios, objects are created with their collection properties already initialized (they start out as an empty collection instead of being null).  This doesn't mean you have to manually call foo.Bar.Add after creating foo, though 🙂

    void Main()


       var foo = new HasPreInitializedCollectionProperties()


           SomeDictionary =


               { 123, "foo" },

               { 456, "bar" },


           SomeList =









    public class HasPreInitializedCollectionProperties


       private readonly Dictionary<int, string> _dictionary = new Dictionary<int, string>();

       public Dictionary<int, string> SomeDictionary { get { return _dictionary; } }

       private readonly List<string> _list = new List<string>();

       public List<string> SomeList { get { return _list; } }


  3. dmitrypodrezoff says:

    This also works with more sophisticated collections, like Dictionary. It helps prevent code like this:

    string longName = string.Empty;

    switch (shortName)


       case "pt1":

           longName = "Part 1";


       case "pt2":

           longName = "Part 2";






  4. Yuri Astrakhan says:

    Unfortunately this feature has one huge drawback: it does not work with extension methods. Without it, one must create custom collection classes, whereas the true power of this would be to use this:

    void Add(this List<MyClass> values, int val1, char val2)

    { values.Add(new MyClass(val1, val2)); }

  5. Jacob Carpenter says:

    Actually you did know about this in September of 2007.…/c-30-collection-initializers-duck.html 🙂

    The pattern matching (duck typing?) rules require the type has to implement IEnumerable and an Add method for collection initialization syntax to be supported.

    At the time you ended by wondering why IEnumerable was required. I think it's because the language designers didn't want to add collection initialization syntax support for purely mathematical methods. See Mads' post:…/what-is-a-collection_3f00_.aspx

  6. Kirill Osenkov says:

    Jacob: LOL, yes, but at that time I didn't realize the Add method can have arbitrary signature 🙂

Skip to main content