国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IEnumerable、GetEnumerator、IEnumerator之間的關系。

2019-11-14 16:44:26
字體:
來源:轉載
供稿:網友
了解了這些也就明白了遍歷的原理,晚安。

  1 using System;  2 using System.Collections;  3   4 public class Person  5 {  6     public Person(string fName, string lName)  7     {  8         this.firstName = fName;  9         this.lastName = lName; 10     } 11  12     public string firstName; 13     public string lastName; 14 } 15  16 public class People : IEnumerable 17 { 18     PRivate Person[] _people; 19     public People(Person[] pArray) 20     { 21         _people = new Person[pArray.Length]; 22  23         for (int i = 0; i < pArray.Length; i++) 24         { 25             _people[i] = pArray[i]; 26         } 27     } 28  29     IEnumerator IEnumerable.GetEnumerator() 30     { 31        return (IEnumerator) GetEnumerator(); 32     } 33  34     public PeopleEnum GetEnumerator() 35     { 36         return new PeopleEnum(_people); 37     } 38 } 39  40 public class PeopleEnum : IEnumerator 41 { 42     public Person[] _people; 43  44     // Enumerators are positioned before the first element 45     // until the first MoveNext() call. 46     int position = -1; 47  48     public PeopleEnum(Person[] list) 49     { 50         _people = list; 51     } 52  53     public bool MoveNext() 54     { 55         position++; 56         return (position < _people.Length); 57     } 58  59     public void Reset() 60     { 61         position = -1; 62     } 63  64     object IEnumerator.Current 65     { 66         get 67         { 68             return Current; 69         } 70     } 71  72     public Person Current 73     { 74         get 75         { 76             try 77             { 78                 return _people[position]; 79             } 80             catch (IndexOutOfRangeException) 81             { 82                 throw new InvalidOperationException(); 83             } 84         } 85     } 86 } 87  88 class App 89 { 90     static void Main() 91     { 92         Person[] peopleArray = new Person[3] 93         { 94             new Person("John", "Smith"), 95             new Person("Jim", "Johnson"), 96             new Person("Sue", "Rabon"), 97         }; 98  99         People peopleList = new People(peopleArray);100         foreach (Person p in peopleList)101             Console.WriteLine(p.firstName + " " + p.lastName);102 103     }104 }105 106 /* This code produces output similar to the following:107  *108  * John Smith109  * Jim Johnson110  * Sue Rabon111  *112  */

 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柞水县| 永修县| 栾城县| 静海县| 济阳县| 垣曲县| 昆明市| 新巴尔虎左旗| 五莲县| 中牟县| 张家港市| 工布江达县| 东源县| 纳雍县| 郁南县| 丹棱县| 衡山县| 夹江县| 巴中市| 安图县| 达拉特旗| 黑龙江省| 天等县| 呼和浩特市| 庆城县| 邢台县| 和田市| 汪清县| 大连市| 洪江市| 富宁县| 仁布县| 香港| 商南县| 剑河县| 靖安县| 米脂县| 临湘市| 雷山县| 宣恩县| 涪陵区|