IEnumerable IEnumerator 自己定义迭代器

   public class Person
    {
        string Name;
        int Age;

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public override string ToString()
        {
            return "Name: " + Name + "	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()
        {
            return new PeopleEnum(_people);
        }
    }

  public class PeopleEnum : IEnumerator
    {
        private Person[] _people;
        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
            {
                return _people[position];
            }
        }

    }
原文地址:https://www.cnblogs.com/lemonP/p/7059551.html