Mega Code Archive

 
Categories / C# / Class Interface
 

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];         }     } }