Jack's Notes5——foreach的本质(通过IEnumerable接口实现遍历)
2012-06-24 19:46
639 查看
class
Program
{
//5、Main函数中的内容是foreach的内部实现过程
static
void Main(string[] args)
{
//5.1实例化需要遍历的类,调用GetEnumerator()方法获得其可遍历的对象
MyColors colors = new MyColors();
IEnumerator colorEnumerator = colors.GetEnumerator();
//5.2使用MoveNext()方法和Current属性通过while循环实现foreach遍历
while (colorEnumerator.MoveNext())
{
Console.WriteLine(colorEnumerator.Current);
}
Console.ReadKey();
}
}
//1、定义一个实现了IEnumerable接口的类
class
MyColors:IEnumerable
{
public
string[] Colors = { "Red",
"Yellow", "Blue" };
//2、该类有GetEnumerator()方法,返回一个实现了IEnumerator接口的类,将需要遍历的值传入该类
public IEnumerator GetEnumerator()
{
return
new ColorEnumerator(Colors);
}
}
//3、该类实现了IEnumerator的Current属性和MoveNext()、Reset()方法,用于实现foreach遍历
class
ColorEnumerator:IEnumerator
{
string[] enumColors;
int index = -1;
//4、重写构造函数,使需要遍历的值可以通过构造函数传进来
public ColorEnumerator(string[] colors)
{
enumColors = new
string[colors.Length];
for (int i = 0; i < enumColors.Length; i++)
{
enumColors[i] = colors[i];
}
}
public
object Current
{
get {
return enumColors[index]; }
}
public
bool MoveNext()
{
if (index < enumColors.Length - 1)
{
index++;
return
true;
}
else
{
return
false;
}
}
public
void Reset()
{
index = -1;
}
}
Program
{
//5、Main函数中的内容是foreach的内部实现过程
static
void Main(string[] args)
{
//5.1实例化需要遍历的类,调用GetEnumerator()方法获得其可遍历的对象
MyColors colors = new MyColors();
IEnumerator colorEnumerator = colors.GetEnumerator();
//5.2使用MoveNext()方法和Current属性通过while循环实现foreach遍历
while (colorEnumerator.MoveNext())
{
Console.WriteLine(colorEnumerator.Current);
}
Console.ReadKey();
}
}
//1、定义一个实现了IEnumerable接口的类
class
MyColors:IEnumerable
{
public
string[] Colors = { "Red",
"Yellow", "Blue" };
//2、该类有GetEnumerator()方法,返回一个实现了IEnumerator接口的类,将需要遍历的值传入该类
public IEnumerator GetEnumerator()
{
return
new ColorEnumerator(Colors);
}
}
//3、该类实现了IEnumerator的Current属性和MoveNext()、Reset()方法,用于实现foreach遍历
class
ColorEnumerator:IEnumerator
{
string[] enumColors;
int index = -1;
//4、重写构造函数,使需要遍历的值可以通过构造函数传进来
public ColorEnumerator(string[] colors)
{
enumColors = new
string[colors.Length];
for (int i = 0; i < enumColors.Length; i++)
{
enumColors[i] = colors[i];
}
}
public
object Current
{
get {
return enumColors[index]; }
}
public
bool MoveNext()
{
if (index < enumColors.Length - 1)
{
index++;
return
true;
}
else
{
return
false;
}
}
public
void Reset()
{
index = -1;
}
}
相关文章推荐
- 利用 IEnumerable接口 实现自定义类型的集合的foreach遍历
- 实现了IEnumerable接口的GetEnumerator 即可使用 Foreach遍历,返回一个IEnumerator对象
- IEnumerable 接口 实现foreach 遍历 实例
- C# 通过接口IEnumerator让自己编写的对象/类,实现foreach遍历方法
- 通过接口实现压缩文件、目录,修正遍历方式,未添加排除文件操作
- IEnumerable 接口 实现foreach
- 如何自己实现IEnumerable和IEnumerator接口以支持foreach语句
- 通过IEnumerable接口遍历数据
- Android实战简易教程<五十四>(通过实现OnScrollListener接口实现下拉刷新功能)
- Android实战简易教程<五十三>(通过实现OnScrollListener接口实现上拉加载更多功能)
- 能用foreach遍历访问的对象需要实现____接口或声明____方法的类型
- Jack's Notes11——打地鼠(forin遍历、type获取节点对象的类型)
- 实现IEnumerable和IEnumerator接口以支持foreach语句
- Jack's Notes8——委托的使用(用三层实现TreeView)
- c#自定义简单链表通过IEnumerable接口,范型实现内部迭代
- 能用foreach遍历访问的对象需要实现____接口或声明____方法的类型
- C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能
- Jack's Notes12——全选、全不选、反选(循环遍历给按钮动态注册事件)
- 能用foreach遍历访问的对象需要实现 ________________接口或声明________________方法的类型。
- C#的foreach可以遍历不继承IEnumerable接口的集合