在C#中,若要对自定义类型对象使用foreach进行迭代的话,该对象必须实现IEnumerable接口下的GetEnumerator方法,而GetEnumerator方法需返回一个IEnumerator的实例,IEnumerator的实例具体通过MoveNext(),Reset(),Current实现了迭代。
我们先来看看IEnumerable和IEnumerator两个接口的结构:
- public interface IEnumerable
- {
- // 返回一个循环访问集合的枚举器。
- [DispId(-4)]
- IEnumerator GetEnumerator();
- }
- public interface IEnumerator
- {
- // 获取集合中的当前元素。
- object Current { get; }
- // 将枚举数推进到集合的下一个元素。
- bool MoveNext();
- // 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
- void Reset();
- }
C#1中实现迭代:
在C#1时,手写迭代是一件非常痛苦的事情,像MoveNext(),Reset(),Current都是要自己动手。
实例如:
- 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 = pArray;
- }
- public IEnumerator GetEnumerator()
- {
- return new PeopleEnum(_people);
- }
- }
- public class PeopleEnum : IEnumerator
- {
- public Person[] _people;
- 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();
- }
- }
- }
- }
调用:
- Person[] peopleArray = new Person[3]
- {
- new Person("Yizhi", "Li"),
- new Person("Wei", "Liu"),
- new Person("Kenny", "Li")
- };
- People peopleList = new People(peopleArray);
- foreach (Person p in peopleList)
- {
- Console.WriteLine(p.firstName + " " + p.lastName);
- }
C#2中实现迭代:
C#2后有了关键字“yield ”后,要实现就变得简单很多,在刚才例子中,将PeopleEnum移除,在People类中的GetEnumerator()方法里代码更改为:for (int i = 0; i < _people.Length; i++){ yield return _people[i];} 即可。
- public IEnumerator GetEnumerator()
- {
- //return new PeopleEnum(_people);
- for (int i = 0; i < _people.Length; i++)
- {
- yield return _people[i];
- }
- }
简单的四行代码就替换了C#1中整个PeopleEnum类的实现,调用代码不变。
注意,yield return 语句只表示“暂时地”退出方法——事实上,可把它当作暂停。