鸟语天空
yield return
post by:追风剑情 2023-7-10 15:04

using System;
using System.Collections.Generic;

namespace TestEnumerable
{
    public class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 0, 2, 3, 4, 5, 6, 7 };
            foreach(int i in GetAllEvenNumber(arr1)) {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }

        //返回集合中的所有偶数
        public static IEnumerable<int> GetAllEvenNumber(IEnumerable<int> source)
        {
            foreach(int item in source)
            {
                if(item % 2 == 0)
                    yield return item;
            }
        }
    }
}

1111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容