static void Main(string[] args) { Collection collection = new Collection(); collection[0] = new Item("Item 0"); collection[1] = new Item("Item 1"); collection[2] = new Item("Item 2"); collection[3] = new Item("Item 3"); collection[4] = new Item("Item 4"); collection[5] = new Item("Item 5"); collection[6] = new Item("Item 6"); collection[7] = new Item("Item 7"); collection[8] = new Item("Item 8"); // Create iterator Iterator iterator = collection.CreateIterator(); // Skip every other item iterator.Step = 2; Console.WriteLine("Iterating over collection:"); for (Item item = iterator.First(); !iterator.IsDone; item = iterator.Next()) { Console.WriteLine(item.Name); } // Wait for user Console.ReadKey(); }
static void Main(string[] args) { Collection collection = new Collection(); collection[0] = new Item("Item 0"); collection[1] = new Item("Item 1"); collection[2] = new Item("Item 2"); collection[3] = new Item("Item 3"); collection[4] = new Item("Item 4"); collection[5] = new Item("Item 5"); collection[6] = new Item("Item 6"); collection[7] = new Item("Item 7"); collection[8] = new Item("Item 8"); IIterator iterator = collection.CreateIterator(); iterator.Step = 2; Console.WriteLine("Iterating over collection:"); for (Item item = iterator.First(); !iterator.HasNext; item = iterator.Next()) { Console.WriteLine(item.Name); } Console.ReadKey(); }
static void Main(string[] args) { //Construir coleccion Collection collection = new Collection(); collection[0] = new Item("Item 0"); collection[1] = new Item("Item 1"); collection[2] = new Item("Item 2"); collection[3] = new Item("Item 3"); collection[4] = new Item("Item 4"); collection[5] = new Item("Item 5"); collection[6] = new Item("Item 6"); collection[7] = new Item("Item 7"); collection[8] = new Item("Item 8"); // Crear iterador Iterator iterator = collection.CreateIterator(); // Omitir cualquier otro elemento iterator.Step = 2; Console.WriteLine("Iterating over collection:"); for (Item item = iterator.First(); !iterator.IsDone; item = iterator.Next()) { Console.WriteLine(item.Name); } // Espera al usuario Console.ReadKey(); }
public static void Main() { var collection = new Collection(); collection[0] = new Item("Item 1"); collection[1] = new Item("Item 2"); collection[2] = new Item("Item 3"); collection[3] = new Item("Item 4"); collection[4] = new Item("Item 5"); var iterator = collection.CreateIterator(); iterator.Step = 2; for (var item = iterator.First(); !iterator.IsDone; item = iterator.Next()) { Console.WriteLine(item.Name); } }
public static void Run() { Console.Write("Iterator Real World Practice"); Collection collection = new Collection(); collection[0] = new Item("Item 0"); collection[1] = new Item("Item 1"); collection[2] = new Item("Item 2"); collection[3] = new Item("Item 3"); collection[4] = new Item("Item 4"); collection[5] = new Item("Item 5"); collection[6] = new Item("Item 6"); collection[7] = new Item("Item 7"); collection[8] = new Item("Item 8"); Iterator iterator = collection.CreateIterator(); iterator.Step = 2; Console.WriteLine("\nIterating over collection"); for (Item item1 = iterator.First(); !iterator.IsDone; item1 = iterator.Next()) { Console.WriteLine(item1.Name); } }
public static void Run() { Console.WriteLine("This real-world code demonstrates the Iterator pattern which is used to iterate over a collection of items and skip a specific number of items each iteration."); Collection collection = new Collection(); collection[0] = new Item("Item 0"); collection[1] = new Item("Item 1"); collection[2] = new Item("Item 2"); collection[3] = new Item("Item 3"); collection[4] = new Item("Item 4"); collection[5] = new Item("Item 5"); collection[6] = new Item("Item 6"); collection[7] = new Item("Item 7"); collection[8] = new Item("Item 8"); Iterator iterator = collection.CreateIterator(); iterator.Step = 2; Console.WriteLine("Iterating over collection"); for (Item item1 = iterator.First(); !iterator.IsDone; item1 = iterator.Next()) { Console.WriteLine(item1.Name); } /* * Iterating over collection: * Item 0 * Item 2 * Item 4 * Item 6 * Item 8 */ }