Hacker News new | ask | show | jobs
by statictype 5890 days ago
I suspect the reason this works this way is because `foreach` is a language construct - not a part of the .NET Framework Class Library. On the other hand, `IEnumerable` is an interface defined in the class library that ships as part of the .NET runtime. Having the foreach construct require implementing IEnumerable will tie the compiler to a specific class definition. So instead, it only looks for a method with that name, which happens to be formalized in the class library in the form of an interface called IEnumerable.

Edit: hm.. but I note that this doesn't work for the using(){} construct.

This requires the object to implement an interface that is convertable to IDisposable instead of just looking for a `Dispose` method.