Foreaching over collections with compatible IEnumerable<T>
implementations is disallowed by the compiler
#76598
Labels
Milestone
(Thanks to @svick at dotnet/csharpstandard#1188)
In C# language version 5, the spec was updated to allow foreaching over collections with multiple
IEnumerable<T>
implementations so long as the chosen implementation could be implicitly converted to all the others. I can see the rationale in this spec change: there is really no confusion about "which sequence" you're getting. The sequence enumerated with the more-derived item type should contain the same instances as when enumerating with the less-derived. If there is any confusion, that's poor type authoring.However, the compiler doesn't seem like it was updated to match this spec change. It continues to implement the behavior as specified in v4:
V5 spec, which allows the above example without compile errors, with an element type of
string
, which is identical to the current spec:V4 spec (PDF), which the compiler still follows:
The text was updated successfully, but these errors were encountered: