How to create a list class to implement ienumerable and yield and implement iteration

From , 5 Years ago, written in C#, viewed 190 times.
URL https://pastebin.vip/view/ce052ea9
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace Yield
  7. {
  8.     class Yield
  9.     {
  10.         public static class NumberList
  11.         {
  12.             // 创建一个整数数组。
  13.             public static int[] ints = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
  14.  
  15.             // 定义一个仅返回偶数的属性。
  16.             public static IEnumerable<int> GetEven()
  17.             {
  18.                 // 使用 yield 返回列表中的偶数。
  19.                 foreach (int i in ints)
  20.                     if (i % 2 == 0)
  21.                         yield return i;
  22.             }
  23.  
  24.             // 定义一个仅返回偶数的属性。
  25.             public static IEnumerable<int> GetOdd()
  26.             {
  27.                 // 使用 yield 仅返回奇数。
  28.                 foreach (int i in ints)
  29.                     if (i % 2 == 1)
  30.                         yield return i;
  31.             }
  32.         }
  33.  
  34.         static void Main(string[] args)
  35.         {
  36.  
  37.             // 显示偶数。
  38.             Console.WriteLine("Even numbers");
  39.             foreach (int i in NumberList.GetEven())
  40.                 Console.WriteLine(i);
  41.  
  42.             // 显示奇数。
  43.             Console.WriteLine("Odd numbers");
  44.             foreach (int i in NumberList.GetOdd())
  45.                 Console.WriteLine(i);
  46.         }
  47.     }
  48. }
  49.  
  50. //csharp/4932

Reply to "How to create a list class to implement ienumerable and yield and implement iteration"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website