yield return

作者:追风剑情 发布于:2023-7-10 15:04 分类:C#

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace TestEnumerable
  5. {
  6. public class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] arr1 = { 0, 2, 3, 4, 5, 6, 7 };
  11. foreach(int i in GetAllEvenNumber(arr1)) {
  12. Console.WriteLine(i);
  13. }
  14. Console.ReadLine();
  15. }
  16.  
  17. //返回集合中的所有偶数
  18. public static IEnumerable<int> GetAllEvenNumber(IEnumerable<int> source)
  19. {
  20. foreach(int item in source)
  21. {
  22. if(item % 2 == 0)
  23. yield return item;
  24. }
  25. }
  26. }
  27. }

1111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号