Looking at this code :
public class myWords : IEnumerable<string>
{
string[] f = "I love you".Split(new string[]{"lo"},StringSplitOptions.RemoveEmptyEntries);
public IEnumerator<string> GetEnumerator()
{
return f.Select(s => s + "2").GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return f.Select(s => s + "3").GetEnumerator();
}
}
Running :
myWords m = new myWords();
foreach (var s in m)
{
Console.WriteLine(s);
}
Yields
I 2
ve you2 // notice "2", so the generic Ienumerator has executed.
I understand that the non-generic IEnumerator
version is for compatibility.
Question:
- In what scenario will the non-generic be invoked?
- How can I force my code to be run with the non-generic
IEnumerator
?