Created
August 4, 2014 22:13
-
-
Save Fhernd/b864009a1ea3f767bf05 to your computer and use it in GitHub Desktop.
Demostración de uso de las interfaces IEnumerator, y IEnumerable en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
namespace Articulos.Cap04 | |
{ | |
// Representa a una persona con propiedades básicas: | |
public class Persona | |
{ | |
public String Nombre | |
{ | |
get; | |
set; | |
} | |
public String Apellido | |
{ | |
get; | |
set; | |
} | |
public Persona(string Nombre, string apellido) | |
{ | |
this.Nombre = Nombre; | |
this.Apellido = apellido; | |
} | |
} | |
// Estructura de datos Gente: | |
public class Gente : IEnumerable | |
{ | |
// Conjunto de personas: | |
private Persona[] personas; | |
public Gente(Persona[] personas) | |
{ | |
this.personas = new Persona[personas.Length]; | |
for (int i = 0; i < personas.Length; ++i) | |
{ | |
this.personas[i] = personas[i]; | |
} | |
} | |
// Implementación explícita de GetEnumerator de IEnumerable: | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return (IEnumerator) GetEnumerator(); | |
} | |
public GenteEnum GetEnumerator() | |
{ | |
return new GenteEnum(personas); | |
} | |
} | |
// Enumerador de los elementos de la estructura Gente: | |
public class GenteEnum : IEnumerator | |
{ | |
public Persona[] gente; | |
// El índice de los enumeradors empieza en la ubicación | |
// anterior al primer elemento de la estructura: | |
int posicion = -1; | |
public GenteEnum(Persona[] gente) | |
{ | |
this.gente = gente; | |
} | |
// Implementa la propiedad Current para obtener el elemento | |
// actual de la colección: | |
object IEnumerator.Current | |
{ | |
get | |
{ | |
return Current; | |
} | |
} | |
public Persona Current | |
{ | |
get | |
{ | |
try | |
{ | |
return gente[posicion]; | |
} | |
catch (IndexOutOfRangeException) | |
{ | |
throw new InvalidOperationException(); | |
} | |
} | |
} | |
// Implementación del método MoveNext() para validar el avance | |
// al siguiente elemento de la estructura de datos: | |
public bool MoveNext() | |
{ | |
++posicion; | |
return ( posicion < gente.Length); | |
} | |
// Implementación del método Reset para reestablecer | |
// el enumerador en la posición inicial: | |
public void Reset() | |
{ | |
posicion = -1; | |
} | |
} | |
public sealed class UsoEnumeradores | |
{ | |
public static void Main() | |
{ | |
// Listado de personas: | |
Persona[] personas = new Persona[3] | |
{ | |
new Persona("Edward", "Ortiz"), | |
new Persona("German", "Ortiz"), | |
new Persona("Daniela", "Ortiz") | |
}; | |
Gente gente = new Gente(personas); | |
// Recorrido por los elementos de la estructura | |
// de personas (Gente): | |
foreach (Persona p in gente) | |
{ | |
Console.WriteLine ("Nombre: {0}, Apellido: {1}", p.Nombre, p.Apellido); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment