在C#中,一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回IEnumerator object)。
枚举数只允许读取集合中的数据。枚举数无法用于修改基础集合。
函数原型如下:
public interface IEnumerable
{
//IEnumerable只有一个方法,返回可循环访问集合的枚举数。
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
// 方法
//移到集合的下一个元素。如果成功则返回为 true;如果超过集合结尾,则返回false。
bool MoveNext();
// 将集合设置为初始位置,该位置位于集合中第一个元素之前
void Reset();
// 属性:获取集合中的当前元素
object Current { get; }
}
//一个结构体,用于类myEnum
struct point
{
public int x;
public int y;
public int z;
}
//我们的一个派生于IEnumerable和IEnumerator接口的自定义类
class myEnum :IEnumerable,IEnumerator
{
//定义索引
private int index;
//定义一个point结构的数组
private point[] points;
//类的构造函数,用于初始化point结构数组
public myEnum(int numofpoint)
{
this.index = -1;
points = new point[numofpoint];
for (int i = 0; i < points.Length ; i++)
{
points[i].x = i;
points[i].y = i*i;
points[i].z = i*i*i;
}
}
//实现IEnumerable接口的GetEnumerator方法,返回一个IEnumerator,这里返回我们的自定义类,因为要对这个类的对象进行迭代
public IEnumerator GetEnumerator()
{
//这里太让我激动了,原来就是返回this这么简单,因为this本身已经是一个ienumerator了,
//不过唯一的问题在于当重复迭代这个对象时,index整型字段还原成-1的问题,
//所以要在movenext()方法return false之前还原index的值。
return (IEnumerator)this; //return points.GetEnumerator(); 均可
}
//实现IEnumerator接口的Reset方法,将集合索引置于第一个元素之前
//枚举数被定位于集合中第一个元素的前面。Reset 也将枚举数返回到此位置。在此位置,调用 Current 会引发异常。因此,在读取 Current 的值之前,必须调用 MoveNext 将枚举数提前到集合的第一个元素。
public void Reset()
{
index = -1;
}
//实现IEnumerator接口的Current属性,返回一个自定义的point结构,即point数组的第index元素
//在调用 MoveNext 或 Reset 之前,Current 返回同一对象。MoveNext 将 Current 设置为下一个元素。
public object Current
{
get
{
try
{
return points[index];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
//实现IEnumerator接口的MoveNext方法,用于向前访问集合元素,如果超出集合范围,返回false
//在传递到集合的末尾之后,枚举数放在集合中最后一个元素后面,且调用 MoveNext 会返回 false。如果最后一次调用 MoveNext 返回 false,则调用 Current 会引发异常。若要再次将 Current 设置为集合的第一个元素,可以调用 Reset,然后再调用 MoveNext。
public bool MoveNext()
{
index++;
if (index >= points.Length)
{
//再返回false之前还原index的值(-1)
Reset();
return false;
}
else
return true;
}
}
只要集合保持不变,枚举数就将保持有效。如果对集合进行了更改(例如添加、修改或删除元素),则该枚举数将失效且不可恢复,并且下一次对 MoveNext 或 Reset 的调用将引发 InvalidOperationException。如果在 MoveNext 和 Current 之间修改集合,那么即使枚举数已经无效,Current 也将返回它所设置成的元素。
IEnumerable和IEnumerator区别
最新推荐文章于 2024-05-13 16:59:08 发布