Created
August 18, 2014 17:33
-
-
Save Fhernd/99391529d74619368c5f to your computer and use it in GitHub Desktop.
Ejemplo de uso de iteradores 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.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