I was looking at C# collection initializers and found the implementation to be very pragmatic but also very unlike anything else in C#
I am able to create code like this:
using System;
using System.Collections;
class Program
{
static void Main()
{
Test test = new Test { 1, 2, 3 };
}
}
class Test : IEnumerable
{
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
public void Add(int i) { }
}
Since I have satisfied the minimum requirements for the compiler (implemented IEnumerable
and a public void Add
) this works but obviously has no value.
I was wondering what prevented the C# team from creating a more strict set of requirements? In other words why, in order for this syntax to compile, does the compiler not require that the type implement ICollection
? That seems more in the spirit of other C# features.