iterates two collections simultaneously : IEnumerator « Class Interface « C# / C Sharp






iterates two collections simultaneously

 

using System;
using System.Collections.Generic;

public class Starter {
    public static void Main() {
        MyClass obj = new MyClass();
        foreach (int item in obj) {
            Console.Write(item);
        }
    }
}

public class MyClass {

    private int[] list1 = new int[] { 0, 2, 4, 6, 8 };
    private int[] list2 = new int[] { 1, 3, 5, 7, 9 };

    public IEnumerator<int> GetEnumerator() {
        for (int index = 0; index < 4; ++index) {
            yield return list1[index];
            yield return list2[index];
        }
    }

}

 








Related examples in the same category

1.yield IEnumerator
2.IEnumerator Example (Static Collection)
3.Collection is iterated using the IEnumerator interface