using System; using System.Collections.Generic; namespace cap07.coleccionenteros { class Lista : IEnumerable { T[] elementos = null; int indice = 0; public Lista(int n) { elementos = new T[n]; } public void AgregarElemento(T elemento) { elementos[indice] = elemento; ++indice; } // Implementación GetEnumerator genérico: public IEnumerator GetEnumerator() { foreach(T elemento in elementos) { if(elemento == null) { break; } yield return elemento; } } // Implementación GetEnumerator no-genérico: System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } public class TestLista { public static void Main() { Lista tresMaestros = new Lista(3); tresMaestros.AgregarElemento("Dostoevsky"); tresMaestros.AgregarElemento("Balzac"); tresMaestros.AgregarElemento("Dickens"); foreach(String maestro in tresMaestros) { Console.WriteLine(maestro); } } } }