yield return 返回迭代器

Posted myblogou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yield return 返回迭代器相关的知识,希望对你有一定的参考价值。

 public class Person
    {
        public string firstName;
        public string lastName;
        public int age;

        public Person(string fName, string lName, int age)
        {
            this.firstName = fName;
            this.lastName = lName;
            this.age = age;
        }
    }

   


    public class People : IEnumerable
    {
        private Person[] _people;
      
        public People(Person[] pArray)
        {
            _people = new Person[pArray.Length];

            for (int i = 0; i < pArray.Length; i++)
            {
                _people[i] = pArray[i];
            }
        }

        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < _people.Length; i++) //int i = _people.Length - 1; i >= 0; --i
            {
                yield return _people[i];
            }
        }
    }

    


    public class PeopleEnum : IEnumerator
    {
        public Person[] _people;

        // Enumerators are positioned before the first element
        // until the first MoveNext() call.

        int position = -1;

        public PeopleEnum(Person[] list)
        {
            _people = list;
        }

        public bool MoveNext()
        {
            position++;
            return (position < _people.Length);
        }

        public void Reset()
        {
            position = -1;
        }

        public object Current
        {
            get
            {
                try
                {
                    return _people[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }


    

    class Program
    {
        static void Main(string[] args)
        {
            Person[] peopleArray = new Person[3]
            {
                new Person("John", "Smith",9),
                new Person("Jim", "Johnson",6),
                new Person("Sue", "Rabon",90)
            };

            People peopleList = new People(peopleArray);

            //People yongPeople;

            //foreach

            //不使用yield return的查找小10岁的算法
            List<Person> allPerson = new List<Person>();
            allPerson.AddRange(
                new Person[3]
                {
                    new Person("John", "Smith",9),
                    new Person("Jim", "Johnson",6),
                    new Person("Sue", "Rabon",90)
                }    
            );

            List<Person> less10Person = new List<Person>();

            foreach(Person p in allPerson)
            {
                if (p.age < 10)
                {
                    less10Person.Add(p);
                }
            }

            foreach (Person p in less10Person)
            {
                Console.WriteLine(p.firstName + "." + p.lastName);
            }

            Console.WriteLine("-----------------------");
            //使用yield return查找年龄小于多少的集合。

            IEnumerable children = GetPeopleByAge(peopleList, 10);

            foreach (Person p in GetPeopleByAge(peopleList, 10))
            {
                Console.WriteLine(p.firstName + "." + p.lastName);
            }

            
        }

        private static IEnumerable GetPeopleByAge(People peopleList, int age)
        {

            foreach (Person p in peopleList)
            {
                if (p.age < age)
                {
                    yield return p;
                }

            }
        }

        

 

以上是关于yield return 返回迭代器的主要内容,如果未能解决你的问题,请参考以下文章

yield return关键字怎么使用?

生成器yield

python中yield与return的用法与区别

关于Python中的yield的理解

生成器

C#中yield用法