C#IEnumerable 接口

IEnumerable 接口
公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。
命名空间:System.Collections

 

  1. public class Person
  2. {
  3. public string firstName;
  4. public string lastName;
  5. public Person(string fName, string lName)
  6. {
  7. this.firstName = fName;
  8. this.lastName = lName;
  9. }
  10. }
  11.  
  12. public class People : IEnumerable
  13. {
  14. private Person[] _person;
  15. public People(Person[] pArray)
  16. {
  17. _person = new Person[pArray.Length];
  18. for (int i = 0; i < pArray.Length; i++)
  19. _person[i] = pArray[i];
  20. }
  21.  
  22. public PeopleEnum GetEnumerator()//返回PeopleEnum当前实例
  23. {
  24. return new PeopleEnum(_person);
  25. }
  26. IEnumerator IEnumerable.GetEnumerator()
  27. {
  28. return (IEnumerator)GetEnumerator();
  29. }
  30. }
  31.  
  32. public class PeopleEnum : IEnumerator
  33. {
  34. int position = -1;
  35.  
  36. public Person[] _person;
  37. public PeopleEnum(Person[] list)
  38. {
  39. _person = list;
  40. }
  41.  
  42. public Person Current
  43. {
  44. get
  45. {
  46. try { return _person[position]; }
  47. catch (IndexOutOfRangeException) { throw new InvalidOperationException(); }
  48. }
  49. }
  50. object IEnumerator.Current
  51. {
  52. get { return Current; }
  53. }
  54.  
  55. public bool MoveNext()
  56. {
  57. position++;
  58. return (position < _person.Length);
  59. }
  60.  
  61. public void Reset()
  62. {
  63. position = -1;
  64. }
  65. }
public class Person
    {
        public string firstName;
        public string lastName;
        public Person(string fName, string lName)
        {
            this.firstName = fName;
            this.lastName = lName;
        }
    }

    public class People : IEnumerable
    {
        private Person[] _person;
        public People(Person[] pArray)
        {
            _person = new Person[pArray.Length];
            for (int i = 0; i < pArray.Length; i++)
                _person[i] = pArray[i];
        }

        public PeopleEnum GetEnumerator()//返回PeopleEnum当前实例
        {
            return new PeopleEnum(_person);
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)GetEnumerator();
        }
    }

    public class PeopleEnum : IEnumerator
    {
        int position = -1;

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

        public Person Current
        {
            get
            {
                try { return _person[position]; }
                catch (IndexOutOfRangeException) { throw new InvalidOperationException(); }
            }
        }
        object IEnumerator.Current
        {
            get { return Current; }
        }

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

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


测试一下:

 

 

  1. static void Main(string[] args)
  2. {
  3. Person[] peopleArray = new Person[3]
  4. {
  5. new Person("John", "Smith"),
  6. new Person("Jim", "Johnson"),
  7. new Person("Sue", "Rabon")
  8. };
  9.  
  10. People peopleList = new People(peopleArray);
  11. foreach (Person p in peopleList)
  12. Console.WriteLine((p.firstName + " " + p.lastName));
  13.  
  14. Console.ReadKey();
  15. }
static void Main(string[] args)
        {
            Person[] peopleArray = new Person[3] 
            { 
                new Person("John", "Smith"),
                new Person("Jim", "Johnson"),
                new Person("Sue", "Rabon")
            };

            People peopleList = new People(peopleArray);
            foreach (Person p in peopleList)
                Console.WriteLine((p.firstName + " " + p.lastName));

            Console.ReadKey();
        }