C#中IEnumerable与IEnumerator接口定义了对集合的简单迭代。IEnumerable是一个声明式的接口,声明实现该接口的类是“可迭代(enumerable)”的,但并没有说明如何实现“迭代器(iterator)”。IEnumerator是一个实现式的接口,实现IEnumerator接口的类就是一个迭代器。
IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,可以通过该方法得到一个迭代器对象。在这个意义上,可以将GetEnumerator()看作IEnumerator的工厂方法也未尝不可。
一般,我们将迭代器作为内部类实现,这样可以尽量少的向外暴露无关的类。
MSDN示例http://msdn.microsoft.com/zh-cn/library/9yb8xew9.aspx
一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回迭代器对象:IEnumerator)。迭代器可用于读取集合中的数据,但不能用于修改基础集合。
实现代码
public class Person {......}
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];
}
IEnumerator GetEnumerator()
{
return
new
PeopleEnum(_people);
}
// 最好使用内部类并且声明为私有类,来实现迭代器,可以对外部封装具体的实现。
private
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
InvalidOperationExceptio
n(); }
}
}
}
最初,枚举数定位在集合中第一个元素前,Reset方法还会将枚举数返回到此位置。在此位置,调用Current属性会引发异常。因此,在读取Current的值之前,必须调用MoveNext方法将枚举数提前到集合的第一个元素。从代码中也可以看出,必须先调用迭代器的MoveNext()方法进行判断是否还有元素可以迭代。等返回true之后,表明还有元素可以迭代,然后才能反问当前元素:Current ,否则可能会报错。
只要集合保持不变,迭代器就保持有效。如果对集合进行了更改(如添加、修改或删除元素),则迭代器将失效且不可恢复。迭代器没有对集合的独占访问权,因此,对集合进行迭代的过程在本质上不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致迭代器引发异常。若要在迭代过程中保证线程安全,可以在整个迭代过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。
实现代码
public class Person {......}
public class People : IEnumerable
{
}