一、Foreach语句简介

在C# 1.0中我们经常使用foreach来遍历一个集合中的元素,然而如果一个集合要支持使用foreach语句来进行遍历,这个集合一般需要IEnumerableIEnumerable<T>接口

因为foreach是迭代语句,要使用foreach必须要有一个迭代器才行的,而IEnumerable接口中的IEnumerator GetEnumerator()方法是返回迭代器的。

在C# 1.0中,要获得迭代器一般需要实现IEnumerable接口中的GetEnumerator()方法,然而要实现一个迭代器就必须实现IEnumerator接口。

在C# 2.0中,提供 yield关键字来简化迭代器的实现。

foreach被编译后会调用GetEnumerator来返回一个迭代器。

 

二、C#1.0中实现迭代器

  1 using System;
  2 using System.Collections;
  3 
  4 namespace 迭代器Demo
  5 {
  6     class Program
  7     {
  8         static void Main(string[] args)
  9         {
 10             Friends friendcollection = new Friends();
 11             foreach (Friend f in friendcollection)
 12             {
 13                 Console.WriteLine(f.Name);
 14             }
 15 
 16             Console.Read();
 17         }
 18     }
 19 
 20     /// <summary>
 21     ///  朋友类
 22     /// </summary>
 23     public class Friend
 24     {
 25         private string name;
 26         public string Name
 27         {
 28             get { return name; }
 29             set { name = value; }
 30         }
 31         public Friend(string name)
 32         {
 33             this.name = name;
 34         }
 35     }
 36 
 37     /// <summary>
 38     ///   朋友集合
 39     /// </summary>
 40     public class Friends : IEnumerable
 41     {
 42         private Friend[] friendarray;
 43 
 44         public Friends()
 45         {
 46             friendarray = new Friend[]
 47             {
 48                 new Friend("张三"),
 49                 new Friend("李四"),
 50                 new Friend("王五")
 51             };
 52         }
 53 
 54         // 索引器
 55         public Friend this[int index]
 56         {
 57             get { return friendarray[index]; }
 58         }
 59 
 60         public int Count
 61         {
 62             get { return friendarray.Length; }
 63         }
 64 
 65         // 实现IEnumerable<T>接口方法
 66        public  IEnumerator GetEnumerator()
 67         {
 68             return new FriendIterator(this);
 69         }
 70     }
 71 
 72     /// <summary>
 73     ///  自定义迭代器,必须实现 IEnumerator接口
 74     /// </summary>
 75     public class FriendIterator : IEnumerator
 76     {
 77         private readonly Friends friends;
 78         private int index;
 79         private Friend current;
 80         internal FriendIterator(Friends friendcollection)
 81         {
 82             this.friends = friendcollection;
 83             index = 0;
 84         }
 85 
 86         #region 实现IEnumerator接口中的方法
 87         public object Current
 88         {
 89             get
 90             {
 91                 return this.current;
 92             }
 93         }
 94 
 95         public bool MoveNext()
 96         {
 97             if (index + 1 > friends.Count)
 98             {
 99                 return false;
100             }
101             else
102             {
103                 this.current = friends[index];
104                 index++;
105                 return true;
106             }
107         }
108 
109         public void Reset()
110         {
111             index = 0;
112         }
113 
114         #endregion 
115     }
116 }
View Code

相关文章:

  • 2021-05-17
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2021-10-18
  • 2022-02-21
  • 2022-12-23
猜你喜欢
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
相关资源
相似解决方案