IEnumerator:提供在普通集合中遍歷的接口,有Current,,MoveNext(),,Reset(),其中Current返回的是object類型,。 2. 在很多地方有討論為什么新增加的泛型接口IEnumerable<T>要繼承IEnumerable,,這是為了兼容。理論上所有的泛型接口都要繼承自所有的非泛型接口,。例如在.net 1.1中有個方法接收的是IEnumerable類型的參數(shù),,當移植到新的環(huán)境下,我們傳入一個IEnumerable<T>的參數(shù),,它也是可以被接受的,,因為他們完成的都是枚舉的行為。 public class Person { public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName; }
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 { public Person[] _people; // Enumerators are positioned before the first element // until the first MoveNext() call. 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 { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } }
public class People : IEnumerable, IEnumerator { private Person[] _people; int position = -1; public People(Person[] pArray) { _people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } } #region IEnumerable Members public IEnumerator GetEnumerator() { return this; } #endregion #region IEnumerator Members public object Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new IndexOutOfRangeException(); } } } public bool MoveNext() { position++; return (position < _people.Length); } public void Reset() { position = -1; } #endregion }
public class People : IEnumerable<Person>, IEnumerator<Person> { private Person[] _people; int position = -1; public People(Person[] pArray) { _people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } } #region IEnumerable<Person> Members public IEnumerator<Person> GetEnumerator() { return this; } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { return this; } #endregion #region IEnumerator<Person> Members public Person Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new IndexOutOfRangeException(); } } } #endregion #region IDisposable Members public void Dispose() { } #endregion #region IEnumerator Members object IEnumerator.Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new IndexOutOfRangeException(); } } } public bool MoveNext() { position++; return (position < _people.Length); } public void Reset() { position = -1; } #endregion }
下面介紹yield關(guān)鍵字的用法: 注意兩點:第一,,它只能用在一個iterator的方法中,也就是說這個方法的返回值類型只能是IEnumerable,,IEnumerator,,IEnumerable<T>或IEnumerator<T>;第二,,它只有兩種語法:yield return 表達式,;或者是yield break; foreach (int i in ints) if (i % 2 == 0) foreach (int i in ints) Console.WriteLine(); |
|