using System;
using System.Collections;
using System.Collections.Generic;
class Program {
// *** было: ***
// чтобы тип годился для foreach, ему приходилось нести перечислитель,
// написанный вручную: поле состояния, MoveNext, Current и Reset
class EvenNumbersOld : IEnumerable {
int bound;
public EvenNumbersOld(int bound) { this.bound = bound; }
public IEnumerator GetEnumerator() { return new Enumerator(bound); }
class Enumerator : IEnumerator {
int bound, current;
public Enumerator(int bound) { this.bound = bound; this.current =
0; }
public bool MoveNext() { current += 2; return current <= bound; }
public object Current { get { return current; } }
public void Reset() { current = 0; }
}
}
// *** в версии 2.0: ***
// yield return пишет за нас этот конечный автомат
static IEnumerable<int> EvenNumbers(int bound) {
for (int n = 2; n <= bound; n += 2)
yield return n; // здесь метод приостанавливается
}
static IEnumerable<int> FirstThree(IEnumerable<int> source) {
int taken = 0;
foreach (int n in source) {
if (taken == 3) yield break; // досрочная остановка
taken++;
yield return n;
}
}
static void Main() {
foreach (int n in new EvenNumbersOld(10)) Console.Write(n + " ");
Console.WriteLine();
foreach (int n in EvenNumbers(10)) Console.Write(n + " ");
Console.WriteLine();
// значения выдаются по одному, поэтому бесконечный источник не страшен
foreach (int n in FirstThree(EvenNumbers(int.MaxValue))) Console.Write(
n + " ");
Console.WriteLine();
}
}