您的位置:首页 > 其它

如何自己实现IEnumerable和IEnumerator接口以支持foreach语句

2007-10-25 18:21 756 查看
在C#中,凡是实现了IEnumerator接口的数据类型都可以用foreach语句进行迭代访问,可是,对于自定义类型如何实现这个接口以支持foreach的迭代呢?要实现这个功能,先来看看IEnumerableIEnumerator接口的定义:

public interface IEnumerable
{
//IEnumerable只有一个方法,返回可循环访问集合的枚举数。
IEnumerator GetEnumerator() ;
}



public interface IEnumerator
{
// 方法
//移到集合的下一个元素。如果成功则返回为 true;如果超过集合结尾,则返回false
bool MoveNext();

// 将集合设置为初始位置,该位置位于集合中第一个元素之前
void Reset();

// 属性:获取集合中的当前元素
object Current { get; }
}

如果我们的自定义数据类型派生于这两个接口,并实现接口中的方法,即可用foreach进行迭代。

下面是一个实现的简单例子:

using System;
using System.Collections ;
using System.Collections.Generic;
using System.Text;

namespace IEnumberableTest
{
class Program
{
static void Main(string[] args)
{
myEnum enum1 = new myEnum(20);

foreach ( point p in enum1)
{
Console.WriteLine("("+p.x .ToString ()+","+p.y.ToString ()+","+p.z.ToString ()+")");
}

Console.Read();
}
}

//一个结构体,用于类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()
{
return (IEnumerator)this;
}

//实现IEnumerator接口的Reset方法,将集合索引置于第一个元素之前
public void Reset()
{
index = -1;
}

//实现IEnumerator接口的Current属性,返回一个自定义的point结构,即point数组的第index元素
public object Current
{
get
{
return points[index];
}
}

//实现IEnumerator接口的MoveNext方法,用于向前访问集合元素,如果超出集合范围,返回false
public bool MoveNext()
{
index++;
return index >= points.Length ? false : true;
}

}
}

编译这个例子,可以看到运行结果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: