Alvin said:
Will it be possible to have more than one Iterator in a type?
Absolutely. Because it's so easy to write an iterators now (or rather,
soon), hopefully more people will use common iteration patterns in their
code. With the current version of C#, writing an enumerator requires a
certain amount of tribal knowledge.
Given a class called Racers with three exposed iterators:
class Racers: IEnumerable<string>
{
string [] _grid = {"Mickey", "Ali", "Kenzie"};
public IEnumerator<string> GetEnumerator()
{
foreach (string racer in _grid)
yield racer;
}
public IEnumerable<string> Grid
{
get
{
foreach (string racer in _grid)
yield racer;
}
}
public IEnumerable<string> BottomUp()
{
for (int i = _grid.Length - 1; i >= 0; --i)
yield _grid
;
}
}
You can enumeratoe in a variety of ways:
static void Main(string[] args)
{
Racers racers = new Racers();
// Calls GetEnumerator
foreach (string r in racers)
Console.WriteLine(r);
// Calls Grid
foreach (string r in racers.Grid)
Console.WriteLine(r);
// Calls BottomUp
foreach (string r in racers.BottomUp())
Console.WriteLine(r);
Console.ReadLine();
}