一直以来,搞不懂IEnumerable与IEnumerator这两个接口的区别,今天看了一下MSDN并在网上搜了一把关于他们的区别,似乎理解了,但还是弄不懂他们最主要的区别是什么。看看MSDN是怎么定义他们的吧。
2 {
3 IEnumerator GetEnumerator();
4}
5
6 public interface IEnumerator
7 {
8 bool MoveNext();
9 void Reset();
10
11 Object Current { get; }
12}
再贴一个MSDN给出的例子:
1using System;
2using System.Collections;
3public class Person
4{
5 public Person(string fName, string lName)
6 {
7 this.firstName = fName;
8 this.lastName = lName;
9 }
10 public string firstName;
11 public string lastName;
12}
13public class People : IEnumerable
14{
15 private Person[] _people;
16 public People(Person[] pArray)
17 {
18 _people = new Person[pArray.Length];
19 for (int i = 0; i < pArray.Length; i++)
20 {
21 _people[i] = pArray[i];
22 }
23 }
24 public IEnumerator GetEnumerator()
25 {
26 return new PeopleEnum(_people);
27 }
28}
29public class PeopleEnum : IEnumerator
30{
31 public Person[] _people;
32 // Enumerators are positioned before the first element
33 // until the first MoveNext() call.
34 int position = -1;
35 public PeopleEnum(Person[] list)
36 {
37 _people = list;
38 }
39 public bool MoveNext()
40 {
41 position++;
42 return (position < _people.Length);
43 }
44 public void Reset()
45 {
46 position = -1;
47 }
48 public object Current
49 {
50 get
51 {
52 try
53 {
54 return _people[position];
55 }
56 catch (IndexOutOfRangeException)
57 {
58 throw new InvalidOperationException();
59 }
60 }
61 }
62}
63class App
64{
65 static void Main()
66 {
67 Person[] peopleArray = new Person[3]
68 {
69 new Person("John", "Smith"),
70 new Person("Jim", "Johnson"),
71 new Person("Sue", "Rabon"),
72 };
73 People peopleList = new People(peopleArray);
74 foreach (Person p in peopleList)
75 Console.WriteLine(p.firstName + " " + p.lastName);
76 }
77}
78/**//* This code produces output similar to the following:
79 *
80 * John Smith
81 * Jim Johnson
82 * Sue Rabon
83 *
84 */
最后看一段别人的总结,水平高的人应该理解更深吧。我先贴出来供以后参考。
1、一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回IEnumerator object)。
2、IEnumerator object具体实现了iterator(通过MoveNext(),Reset(),Current)。
3、从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举(enumerable)”的,但并没有说明如何实现枚举器(iterator);IEnumerator是一个实现式的接口,IEnumerator object就是一个iterator。
4、IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,client可以通过IEnumerable的GetEnumerator()得到IEnumerator object,在这个意义上,将GetEnumerator()看作IEnumerator object的factory method也未尝不可。
IEnumerator 是所有枚举数的基接口。
枚举数只允许读取集合中的数据。枚举数无法用于修改基础集合。
最初,枚举数被定位于集合中第一个元素的前面。Reset 也将枚举数返回到此位置。在此位置,调用 Current 会引发异常。因此,在读取 Current 的值之前,必须调用 MoveNext 将枚举数提前到集合的第一个元素。
在调用 MoveNext 或 Reset 之前,Current 返回同一对象。MoveNext 将 Current 设置为下一个元素。
在传递到集合的末尾之后,枚举数放在集合中最后一个元素后面,且调用 MoveNext 会返回 false。如果最后一次调用 MoveNext 返回 false,则调用 Current 会引发异常。若要再次将 Current 设置为集合的第一个元素,可以调用 Reset,然后再调用 MoveNext。
只要集合保持不变,枚举数就将保持有效。如果对集合进行了更改(例如添加、修改或删除元素),则该枚举数将失效且不可恢复,并且下一次对 MoveNext 或 Reset 的调用将引发 InvalidOperationException。如果在 MoveNext 和 Current 之间修改集合,那么即使枚举数已经无效,Current 也将返回它所设置成的元素。
枚举数没有对集合的独占访问权;因此,枚举一个集合在本质上不是一个线程安全的过程。甚至在对集合进行同步处理时,其他线程仍可以修改该集合,这会导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。
参考资料 http://www.cnblogs.com/illele/archive/2008/04/21/1164696.html