C#中实现迭代器

    在C#中,若要对自定义类型对象使用foreach进行迭代的话,该对象必须实现IEnumerable接口下的GetEnumerator方法,而GetEnumerator方法需返回一个IEnumerator的实例,IEnumerator的实例具体通过MoveNext(),Reset(),Current实现了迭代。

      我们先来看看IEnumerableIEnumerator两个接口的结构:

[c-sharp]  view plain copy
  1. public interface IEnumerable  
  2. {  
  3.     // 返回一个循环访问集合的枚举器。  
  4.      [DispId(-4)]  
  5.     IEnumerator GetEnumerator();  
  6. }  
  7.   
  8. public interface IEnumerator  
  9. {  
  10.     // 获取集合中的当前元素。  
  11.     object Current { get; }  
  12.   
  13.     // 将枚举数推进到集合的下一个元素。  
  14.      bool MoveNext();  
  15.   
  16.      // 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。  
  17.      void Reset();  
  18. }   

      C#1中实现迭代:

      在C#1时,手写迭代是一件非常痛苦的事情,像MoveNext(),Reset(),Current都是要自己动手。

      实例如:

[c-sharp]  view plain copy
  1. public class Person  
  2. {  
  3.     public Person(string fName, string lName)  
  4.     {  
  5.         this.firstName = fName;     
  6.         this.lastName = lName;  
  7.     }  
  8.     public string firstName;  
  9.     public string lastName;  
  10. }  
  11.   
  12. public class People : IEnumerable  
  13. {  
  14.     private Person[] _people;  
  15.     public People(Person[] pArray)  
  16.     {  
  17.         _people = pArray;  
  18.     }  
  19.     public IEnumerator GetEnumerator()  
  20.     {  
  21.         return new PeopleEnum(_people);  
  22.     }  
  23. }  
  24. public class PeopleEnum : IEnumerator  
  25. {  
  26.     public Person[] _people;  
  27.     int position = -1;  
  28.   
  29.     public PeopleEnum(Person[] list)  
  30.     {  
  31.         _people = list;  
  32.     }  
  33.     public bool MoveNext()  
  34.     {  
  35.         position++;  
  36.         return (position < _people.Length);  
  37.     }  
  38.   
  39.     public void Reset()  
  40.     {  
  41.         position = -1;  
  42.     }  
  43.   
  44.     public object Current  
  45.     {  
  46.         get  
  47.         {  
  48.             try  
  49.             {  
  50.                 return _people[position];  
  51.             }  
  52.             catch (IndexOutOfRangeException)  
  53.             {  
  54.                 throw new InvalidOperationException();  
  55.             }  
  56.         }  
  57.     }  
  58. }   

调用:

[c-sharp]  view plain copy
  1. Person[] peopleArray = new Person[3]   
  2. {   
  3.     new Person("Yizhi""Li"),   
  4.     new Person("Wei""Liu"),   
  5.     new Person("Kenny""Li")    
  6. };  
  7.   
  8. People peopleList = new People(peopleArray);  
  9. foreach (Person p in peopleList)  
  10. {  
  11.     Console.WriteLine(p.firstName + " " + p.lastName);  
  12. }   

      C#2中实现迭代:

      C#2后有了关键字“yield ”后,要实现就变得简单很多,在刚才例子中,将PeopleEnum移除,在People类中的GetEnumerator()方法里代码更改为:for (int i = 0; i < _people.Length; i++){ yield return _people[i];} 即可。

[c-sharp]  view plain copy
  1. public IEnumerator GetEnumerator()  
  2. {  
  3.      //return new PeopleEnum(_people);  
  4.   
  5.      for (int i = 0; i < _people.Length; i++)  
  6.      {  
  7.          yield return _people[i];  
  8.      }  
  9. }  

     简单的四行代码就替换了C#1中整个PeopleEnum类的实现,调用代码不变。

     注意,yield return 语句只表示“暂时地”退出方法——事实上,可把它当作暂停。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值