IEnumerator:提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。 2. 在很多地方有讨论为什么新增加的泛型接口IEnumerable<T>要继承IEnumerable,这是为了兼容。理论上所有的泛型接口都要继承自所有的非泛型接口。例如在.net 1.1中有个方法接收的是IEnumerable类型的参数,当移植到新的环境下,我们传入一个IEnumerable<T>的参数,它也是可以被接受的,因为他们完成的都是枚举的行为。 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关键字的用法: 注意两点:第一,它只能用在一个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(); |
|