Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created August 18, 2014 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/99391529d74619368c5f to your computer and use it in GitHub Desktop.
Save Fhernd/99391529d74619368c5f to your computer and use it in GitHub Desktop.
Ejemplo de uso de iteradores en C#.
using System;
using System.Collections.Generic;
namespace Articulos.Cap04.Iteradores
{
public sealed class IteradorNumerosEnteros
{
public static void Main()
{
Console.WriteLine ();
foreach (int numero in GeneradorNumerosPares(3, 21))
{
Console.Write ("{0} ", numero.ToString());
}
Console.WriteLine ();
Console.ReadLine ();
}
// Genera números pares a partir de un rango de valores:
private static IEnumerable<int> GeneradorNumerosPares(int inferior, int superior)
{
for (int numero = inferior; numero <= superior; ++numero)
{
// Evalúa si el número es par:
if (numero % 2 == 0)
{
yield return numero;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment