Advertisement
Fhernd

Coleccion.cs

Aug 13th, 2017
15,358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace cap07.coleccionenteros
  5. {
  6.     class Lista<T> : IEnumerable<T>
  7.     {
  8.         T[] elementos = null;
  9.         int indice = 0;
  10.  
  11.         public Lista(int n)
  12.         {
  13.             elementos = new T[n];
  14.         }
  15.  
  16.         public void AgregarElemento(T elemento)
  17.         {
  18.             elementos[indice] = elemento;
  19.             ++indice;
  20.         }
  21.  
  22.         // Implementación GetEnumerator genérico:
  23.         public IEnumerator<T> GetEnumerator()
  24.         {
  25.             foreach(T elemento in elementos)
  26.             {
  27.                 if(elemento == null)
  28.                 {
  29.                     break;
  30.                 }
  31.  
  32.                 yield return elemento;
  33.             }
  34.         }
  35.  
  36.         // Implementación GetEnumerator no-genérico:
  37.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  38.         {
  39.             return this.GetEnumerator();
  40.         }
  41.     }
  42.  
  43.     public class TestLista
  44.     {
  45.         public static void Main()
  46.         {
  47.             Lista<String> tresMaestros = new Lista<string>(3);
  48.  
  49.             tresMaestros.AgregarElemento("Dostoevsky");
  50.             tresMaestros.AgregarElemento("Balzac");
  51.             tresMaestros.AgregarElemento("Dickens");
  52.  
  53.             foreach(String maestro in tresMaestros)
  54.             {
  55.                 Console.WriteLine(maestro);
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement