Advertisement
Fhernd

UsoEnumeradores.cs

Aug 4th, 2014
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Articulos.Cap04
  5. {
  6.     // Representa a una persona con propiedades básicas:
  7.     public class Persona
  8.     {
  9.         public String Nombre
  10.         {
  11.             get;
  12.             set;
  13.         }
  14.        
  15.         public String Apellido
  16.         {
  17.             get;
  18.             set;
  19.         }
  20.        
  21.         public Persona(string Nombre, string apellido)
  22.         {
  23.             this.Nombre = Nombre;
  24.             this.Apellido = apellido;
  25.         }
  26.     }
  27.    
  28.     // Estructura de datos Gente:
  29.     public class Gente : IEnumerable
  30.     {
  31.         // Conjunto de personas:
  32.         private Persona[] personas;
  33.        
  34.         public Gente(Persona[] personas)
  35.         {
  36.             this.personas = new Persona[personas.Length];
  37.            
  38.             for (int i = 0; i < personas.Length; ++i)
  39.             {
  40.                 this.personas[i] = personas[i];
  41.             }
  42.         }
  43.        
  44.         // Implementación explícita de GetEnumerator de IEnumerable:
  45.         IEnumerator IEnumerable.GetEnumerator()
  46.         {
  47.             return (IEnumerator) GetEnumerator();
  48.         }
  49.        
  50.         public GenteEnum GetEnumerator()
  51.         {
  52.             return new GenteEnum(personas);
  53.         }
  54.     }
  55.    
  56.     // Enumerador de los elementos de la estructura Gente:
  57.     public class GenteEnum : IEnumerator
  58.     {
  59.         public Persona[] gente;
  60.        
  61.         // El índice de los enumeradors empieza en la ubicación
  62.         // anterior al primer elemento de la estructura:
  63.         int posicion = -1;
  64.        
  65.         public GenteEnum(Persona[] gente)
  66.         {
  67.             this.gente = gente;
  68.         }
  69.        
  70.         // Implementa la propiedad Current para obtener el elemento
  71.         // actual de la colección:
  72.         object IEnumerator.Current
  73.         {
  74.             get
  75.             {
  76.                 return Current;
  77.             }
  78.         }
  79.        
  80.         public Persona Current
  81.         {
  82.             get
  83.             {
  84.                 try
  85.                 {
  86.                     return gente[posicion];
  87.                 }
  88.                 catch (IndexOutOfRangeException)
  89.                 {
  90.                     throw new InvalidOperationException();
  91.                 }
  92.             }
  93.         }
  94.        
  95.         // Implementación del método MoveNext() para validar el avance  
  96.         // al siguiente elemento de la estructura de datos:
  97.         public bool MoveNext()
  98.         {
  99.             ++posicion;
  100.             return ( posicion < gente.Length);
  101.         }
  102.        
  103.         // Implementación del método Reset para reestablecer
  104.         // el enumerador en la posición inicial:
  105.         public void Reset()
  106.         {
  107.             posicion = -1;
  108.         }
  109.     }
  110.    
  111.     public sealed class UsoEnumeradores
  112.     {
  113.         public static void Main()
  114.         {
  115.             // Listado de personas:
  116.             Persona[] personas = new Persona[3]
  117.             {
  118.                 new Persona("Edward", "Ortiz"),
  119.                 new Persona("German", "Ortiz"),
  120.                 new Persona("Daniela", "Ortiz")
  121.             };
  122.            
  123.             Gente gente = new Gente(personas);
  124.            
  125.             // Recorrido por los elementos de la estructura
  126.             // de personas (Gente):
  127.             foreach (Persona p in gente)
  128.             {
  129.                 Console.WriteLine ("Nombre: {0}, Apellido: {1}", p.Nombre, p.Apellido);
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement