Advertisement
Fhernd

IteradorNumerosEnteros.cs

Aug 18th, 2014
2,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Articulos.Cap04.Iteradores
  5. {
  6.     public sealed class IteradorNumerosEnteros
  7.     {
  8.         public static void Main()
  9.         {
  10.             Console.WriteLine ();
  11.            
  12.             foreach (int numero in GeneradorNumerosPares(3, 21))
  13.             {
  14.                 Console.Write ("{0} ", numero.ToString());
  15.             }
  16.            
  17.             Console.WriteLine ();
  18.             Console.ReadLine ();
  19.         }
  20.        
  21.         // Genera números pares a partir de un rango de valores:
  22.         private static IEnumerable<int> GeneradorNumerosPares(int inferior, int superior)
  23.         {
  24.             for (int numero = inferior; numero <= superior; ++numero)
  25.             {
  26.                 // Evalúa si el número es par:
  27.                 if (numero % 2 == 0)
  28.                 {
  29.                     yield return numero;
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement