This project is read-only.
1
Vote

Feature Request: allow GetEnumerator as Extension method for foreach

description

When using a type as collection in a foreach clause, the type needs to have a GetEnumerator method that returns an object that has a MoveNext function with Boolean result and a Current property. Usually this is realized by implementing IEnumerable. However that is not required.

In C#, GetEnumerator should be defined on the collection type itself.

In Vb.Net, retrofitting GetEnumerator by using extension methods is allowed.

Can the C# foreach be made to work like the VB. NET Foreach in such a way that GetEnumerator as Extension method works? If this not a good idea would you be so kind to explain what goes wrong if it was implemented in C# or what in the language or compiler makes impossible to do.

Found by retrofitting .NET tuple classes in Vb. Net first and translate to C# later.

Sample code: https://github.com/nicenemo/Noaber?files=1 implement LINQ for Tuples. Some tests still use VB. NET.

Cross-post from Stackoverflow. Made note to self to not ask Why or Why not questions on Stackoverflow.

comments