如何让一个类可以被foreach枚举?
答:实现IEnumerable或其IEnumerable<T>。该接口只有一个函数 public IEnumerator GetEnumerator();
在这里,IEnumerator也是一个接口,我们将实现该接口的类称之为枚举数(enumerator)。
也就是说,任何一个可以被枚举的类型,都可以通过GetEnumerator()得到这个枚举数;反过来,任何一个类型,若想要其能被枚举,那么必须实现IEnumerator接口的GetEnumerator()方法。
那么,如何实现GetEnumerator?
下面是三个版本的例子
例子1:非泛型
class Program
{
static void Main(string[] args)
{ MyColors myColors=new MyColors();
foreach (var myColor in myColors)
{
Console.WriteLine(myColor);
}
Console.Read(); }
}
class ColorEnumerator : IEnumerator
{
private string[] Colors;
int position = -;
public bool MoveNext()
{
position++;
if (position<Colors.Count())
{
return true;
}
return false;
} public void Reset()
{
position = -; }
public object Current { get { return Colors[position]; } }
public ColorEnumerator(string[] theColors)
{
Colors = new string[theColors.Count()];
for (int i = ; i < theColors.Count(); i++)
{
Colors[i] = theColors[i];
}
} }
class MyColors : IEnumerable
{
private string[] Colors = {"Red","Yellow","Blue"};
public IEnumerator GetEnumerator()
{
return new ColorEnumerator(Colors);
}
}
例子二(泛型的方法):
class ColorEnumerator : IEnumerator<string>
{
private string[] Colors;
int position = -;
public bool MoveNext()
{
position++;
if (position<Colors.Count())
{
return true;
}
return false;
} public void Reset()
{
position = -; } object IEnumerator.Current
{
get { return Current; }
} public string Current
{
get { return Colors[position]; }
} public ColorEnumerator(string[] theColors)
{
Colors = new string[theColors.Count()];
for (int i = ; i < theColors.Count(); i++)
{
Colors[i] = theColors[i];
}
} public void Dispose()
{ }
}
class MyColors : IEnumerable<string>
{
private string[] Colors = { "Red", "Yellow", "Blue" }; public IEnumerator<string> GetEnumerator()
{
return new ColorEnumerator(Colors);
} IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
class Program
{
static void Main(string[] args)
{ MyColors myColors = new MyColors();
foreach (var color in myColors)
{
Console.WriteLine(color);
}
Console.Read();
}
}
例子三(迭代器方法):
class MyColors : IEnumerable
{ public IEnumerator GetEnumerator()
{
yield return "Blue";
yield return "Yellow";
yield return "Red";
} }
如何让一个类可以被foreach枚举?的更多相关文章
- PHP5实现foreach语言结构遍历一个类的实例
PHP5实现foreach语言结构遍历一个类 创建一个类集成Iterator接口,并实现Iterator里面的方法即可,下面见实例代码实现 <?php class Test implements ...
- 在PHP中检测一个类是否可以被foreach遍历
在PHP中,我们可以非常简单的判断一个变量是什么类型,也可以非常方便的确定一个数组的长度从而决定这个数组是否可以遍历.那么类呢?我们要如何知道这个类是否可以通过 foreach 来进行遍历呢?其实,P ...
- 手动写一个类支持foreach循环
之前初学时看过可以实现Iterable接口实现Iterator迭代器的支持,并且也支持foreach循环.现在学习了数据结构,手动写一个单链表支持foreach循环吧. 手写foreach循环步骤: ...
- c# foreach枚举器
要是自己的类支持foreach ,必须在类中必须有GetEnumerator方法,该方法返回的是一个IEnumerator类型的枚举器; public class MyStruct { public ...
- 遍历一个类的属性--并转换为Dictionary类型
参考地址...http://www.cnblogs.com/xwgli/p/3306297.html 记录点滴...以前很少用泛型...HaHa... /// <summary> /// ...
- yii2封装一个类控制div宽度,高度
1.首先,封装一个类,放在文件夹vendor下,命名为articls.php. <?phpclass Articles{ //测试 function add() { r ...
- C#判断一个类中有无"指定名称"的方法
C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?
当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...
- C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值
转自goldeneyezhang原文 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值总结: 对应某个类的 ...
随机推荐
- 数据导入导出Oracle数据库
临近春节,接到了一个导入数据的任务,在Linux客户端中的数据有50G,大约3亿3千万行: 刚开始很天真,把原始的txt/csv文件用sh脚本转化成了oralce 的insert into 语句,然后 ...
- NEERC2014 Eastern subregional
\ 先把furthur的超碉线段树粘过来 //#pragma comment(linker, "/STACK:102400000,102400000") #include<c ...
- Pandas-数据聚合与分组运算
目录 图解"split-apply-combine" 数据的分类split: groupby() 以column进行分组 以index进行分组 分组遍历 数据的应用apply: a ...
- NoClassDefFoundError vs ClassNotFoundException
我们先来认识一下Error 和Exception, 两个都是Throwable类的直接子类. Javadoc 很好的说明了Error类: An Error is a subclass of Thro ...
- Javascript的动态增加‘类’的方法
1.我们可以为每一个实例对象增加方法.也就是说我们在每次使用‘类’之外的方法时候,都需要创建一次. function Dog(){ window.alert('I am a dog!'); } va ...
- 1·3 对 git 的认识
我可以诚实的说:这是我第一次听见这个名词 GIT.老师您发的关于git链接我下载了,只是还没看完.所以以下只是片面的理解,在后期我会单独再发一次. 一·GIT的简单介绍 1·Git是一款免费.开源的分 ...
- php实战开发之自我整理(学习笔记)
PHP没有创建变量的命令,变量会在首次赋值时进行创建. 简单样例 1 <?php $word="My first choice"; $x=5; echo $x; echo & ...
- espcms内容页上下篇按后台手动排序号
模板文件: {%get name=plist class="did":$read.did,pid:$read.pid%} <li class="fl"&g ...
- HTTPS Web配置举例
http://www.h3c.com.cn/Products___Technology/Technology/Security_Encrypt/Other_technology/Representat ...
- 服务器监控之 Monitorix 初体验
参考: http://www.tecmint.com/monitorix-a-lightweight-system-and-network-monitoring-tool-for-linux/ Cen ...