title: Difference Between Collection Initializer Syntaxes
body: <p><a href="https://msdn.microsoft.com/en-us/library/bb384062.aspx" rel="nofollow noreferrer">MSDN</a> states that:</p>
  <blockquote>
  <p>By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.</p>
  </blockquote>
  <p>They also give this example, using the newer collection initialization syntax with brackets:</p>
  <pre><code>var numbers = new Dictionary&lt;int, string&gt; { 
      [7] = &quot;seven&quot;, 
      [9] = &quot;nine&quot;, 
      [13] = &quot;thirteen&quot; 
  };
  </code></pre>
  <p>However, when checking the IL code generated, it seems that this code does not at all result in any calls to the <code>Add</code> method, but to rather to one <code>set_item</code>, like so:</p>
  <pre><code>IL_0007: ldstr        &quot;seven&quot;
  IL_000c: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2&lt;int32, string&gt;::set_Item(!0/*int32*/, !1/*string*/)
  </code></pre>
  <p>The &quot;old&quot; syntax with curly brackets in contrast gives the following:</p>
  <pre><code>// C# code:
  var numbers2 = new Dictionary&lt;Int32, String&gt;
  {
      {7, &quot;seven&quot;},
      {9, &quot;nine&quot;},
      {13, &quot;thirteen&quot;}
  };
  
  // IL code snippet:
  // ----------
  // IL_0033: ldstr        &quot;seven&quot;
  // IL_0038: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2&lt;int32, string&gt;::Add(!0/*int32*/, !1/*string*/)
  </code></pre>
  <p>... As you can see, a call to <code>Add</code> is the result, as expected. (One can only assume that the text on MSDN mentioned above is yet to be updated.)</p>
  <p>I've so far discovered one case where this difference actually matters, and that is with the quirky <code>System.Collections.Specialized.NameValueCollection</code>. This one allows for one key to point to more than one value. Initialization can be done in both ways:</p>
  <pre><code>const String key = &quot;sameKey&quot;;
  const String value1 = &quot;value1&quot;;
  const String value2 = &quot;value2&quot;;
  
  var collection1 = new NameValueCollection
  {
      {key, value1},
      {key, value2}
  };
  
  var collection2 = new NameValueCollection
  {
      [key] = value1,
      [key] = value2
  };
  </code></pre>
  <p>... But because of the differences in how only the former actually calls the <code>NameValueCollection::Add(string, string)</code>, the results differ when looking at the contents of each collection;</p>
  <blockquote>
  <p>collection1[key] = &quot;value1,value2&quot;</p>
  <p>collection2[key] = &quot;value2&quot;</p>
  </blockquote>
  <p>I realize that there's a connection between the old syntax and the <code>IEnumerable</code> interface, and how the compiler finds the <code>Add</code> method by naming convention etcetera. I allso realize the benefits of any indexer type being subject to the new syntax, as discussed in <a href="https://stackoverflow.com/questions/28076127/c6s-new-collection-initializer-clarification">this SO answer</a> before.</p>
  <p>Perhaps these are all expected features, from your point of view, but the implications had not occurred to me, and I'm curious to know more.</p>
  <p>So, I wonder if there's a source of documentation at MSDN or elsewhere that clarifies this difference in behavior that comes with the choice of syntax. I also wonder if you know of any other examples where this choice may have such an impact as when initializing a <code>NameValueCollection</code>.</p>