plina

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
   public class Person
    {
        string Name;
        int Age;

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

        public override string ToString()
        {
            return "Name: " + Name + "\tAge: " + 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];
            }
        }

    }

 

posted on 2017-06-21 14:53  plina  阅读(122)  评论(0编辑  收藏  举报