免费男女视频_国产系列 视频二区_羞羞视频免费入口网站_久国久产久精永久网页_国产免费观看av_一区二区三区日韩在线观看

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();
        }

主站蜘蛛池模板: 在线观看麻豆 | 女18一级大黄毛片免费女人 | 欧美视频在线观看一区 | 成人爽a毛片免费啪啪红桃视频 | 国产成人精品免费视频大全最热 | 国产亚洲精品久久久久5区 综合激情网 | 青草久久av | 亚洲综合视频网站 | 黄色片一区二区 | 久久久久久91| 亚洲一区免费观看 | 小视频在线看 | 欧美日韩一 | av懂色| 深夜免费视频 | 99麻豆久久久国产精品免费 | 久久久国产精品免费观看 | 国产成人av一区 | 亚洲国产精品久久久久久久久久 | 深夜小视频在线观看 | 99999久久久久久 | 美女污污在线观看 | 一区二区三区四区视频在线观看 | 国产午夜精品视频免费不卡69堂 | 欧美伦交 | 国产精品一区2区3区 | 成人免费视频视频在线观看 免费 | 日韩欧美精品电影 | 久久密| av在线免费看网站 | 99视频网址| 中文字幕一区二区三区四区 | 国产免费视频一区二区裸体 | 日本羞羞的午夜电视剧 | 九一免费版在线观看 | 久久久久北条麻妃免费看 | 国产精品www | 日本不卡一区二区三区在线观看 | 欧美性生交xxxxx久久久缅北 | 成人黄视频在线观看 | 91成人在线免费观看 |