A generic iterator. : Generic IEnumerator « Generic « C# / CSharp Tutorial






using System; 
using System.Collections.Generic; 
 
class MyClass<T> { 
  T[] array; 
 
  public MyClass(T[] a) { 
    array = a; 
  } 
 
  public IEnumerator<T> GetEnumerator() { 
    foreach(T obj in array) 
      yield return obj; 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    int[] nums = { 4, 3, 6, 4, 7, 9 }; 
    MyClass<int> mc = new MyClass<int>(nums); 
 
    foreach(int x in mc) 
      Console.Write(x + " "); 
 
    Console.WriteLine(); 
 
 
   bool[] bVals = { true, true, false, true }; 
    MyClass<bool> mc2 = new MyClass<bool>(bVals); 
 
    foreach(bool b in mc2) 
      Console.Write(b + " "); 
 
    Console.WriteLine(); 
  } 
}
4 3 6 4 7 9
True True False True








18.10.Generic IEnumerator
18.10.1.IEnumerator
18.10.2.Creating a list from an array
18.10.3.A generic iterator.
18.10.4.Generic IEnumerator/IEnumerable
18.10.5.Template method