Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your observation is spot on - in fact, it mirrors one made by Mads Torgersen, a Microsoft C# Language PM.</p> <p>Mads made a post in October 2006 on this subject titled <em><a href="http://blogs.msdn.com/madst/archive/2006/10/10/What-is-a-collection_3F00_.aspx" rel="noreferrer">What Is a Collection?</a></em> in which he wrote:</p> <blockquote> <p>Admitted, we blew it in the first version of the framework with System.Collections.ICollection, which is next to useless. But we fixed it up pretty well when generics came along in .NET framework 2.0: System.Collections.Generic.ICollection&lt;T> lets you Add and Remove elements, enumerate them, Count them and check for membership.</p> <p>Obviously from then on, everyone would implement ICollection&lt;T&gt; every time they make a collection, right? Not so. Here is how we used LINQ to learn about what collections really are, and how that made us change our language design in C# 3.0.</p> </blockquote> <p>It turns out that there are only 14 implementations of <code>ICollection&lt;T&gt;</code> in the framework, but 189 classes that implement <code>IEnumerable</code> and have a public <code>Add()</code> method.</p> <p>There's a hidden benefit to this approach - if they had based it on the <code>ICollection&lt;T&gt;</code> interface, there would have been exactly one supported <code>Add()</code> method.</p> <p>In contrast, the approach they did take means that the initializers for the collection just form sets of arguments for the <code>Add()</code> methods.</p> <p>To illustrate, let's extend your code slightly:</p> <pre><code>class Test : IEnumerable { public IEnumerator GetEnumerator() { throw new NotImplementedException(); } public void Add(int i) { } public void Add(int i, string s) { } } </code></pre> <p>You can now write this:</p> <pre><code>class Program { static void Main() { Test test = new Test { 1, { 2, "two" }, 3 }; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload