答:实现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枚举?的更多相关文章

  1. PHP5实现foreach语言结构遍历一个类的实例

    PHP5实现foreach语言结构遍历一个类 创建一个类集成Iterator接口,并实现Iterator里面的方法即可,下面见实例代码实现 <?php class Test implements ...

  2. 在PHP中检测一个类是否可以被foreach遍历

    在PHP中,我们可以非常简单的判断一个变量是什么类型,也可以非常方便的确定一个数组的长度从而决定这个数组是否可以遍历.那么类呢?我们要如何知道这个类是否可以通过 foreach 来进行遍历呢?其实,P ...

  3. 手动写一个类支持foreach循环

    之前初学时看过可以实现Iterable接口实现Iterator迭代器的支持,并且也支持foreach循环.现在学习了数据结构,手动写一个单链表支持foreach循环吧. 手写foreach循环步骤: ...

  4. c# foreach枚举器

    要是自己的类支持foreach ,必须在类中必须有GetEnumerator方法,该方法返回的是一个IEnumerator类型的枚举器; public class MyStruct { public ...

  5. 遍历一个类的属性--并转换为Dictionary类型

    参考地址...http://www.cnblogs.com/xwgli/p/3306297.html 记录点滴...以前很少用泛型...HaHa... /// <summary> /// ...

  6. yii2封装一个类控制div宽度,高度

    1.首先,封装一个类,放在文件夹vendor下,命名为articls.php. <?phpclass Articles{ //测试    function add()    {        r ...

  7. 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 ...

  8. 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?

    当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...

  9. C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值

    转自goldeneyezhang原文 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值总结: 对应某个类的 ...

随机推荐

  1. [NHibernate]HQL查询

    目录 写在前面 文档与系列文章 查询的几种方式 HQL查询 一个例子 总结 写在前面 上篇文章介绍了nhibernate在项目中的基本配置,包括数据库连接字符串的设置,映射文件的配置及需注意的地方,这 ...

  2. 前端工具之Gulp

    Gulp是一款前端自动化的工具,如果能熟练使用Gulp来进行开发一定可以节省很多的时间,也可以快速的提高工作效率. 在使用Gulp之前就是要配置好Gulp安装的环境,这是我们能使用Gulp快速开发的第 ...

  3. svn 版本转为git

    git clone 相当于git init 和 git svn fetch.git svn rease git svn fetch 从svn服务器取指定区间的版本转化成git库 git svn reb ...

  4. Javascript中闭包问题(转载)

    学习Javascript闭包(Closure)   作者: 阮一峰 日期: 2009年8月30日 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现 ...

  5. PHP数据采集curl常用的5个例子

    用php ,curl主要是抓取数据,当然我们可以用其他的方法来抓取,比如fsockopen,file_get_contents等.但是只能抓那些能直接访问的页面,如果要抓取有页面访问控制的页面,或者是 ...

  6. 修改Linux的SSH远程连接端口 技巧

    将SSH终端服务的端口由 22 修改为别的端口以防攻击黑客直接猜解您的服务器密码 首先修改配置文件 vi /etc/ssh/sshd_config 找到 #Port 22 一段,这里是标识默认使用 2 ...

  7. what's cloud computing? IaaS

    Cloud computing has changed the ITC industry. Companies like Amazon, Google and Microsoft have built ...

  8. 二叉树建立,遍历和二叉排序树的判断【c++】

    // test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  9. Linux下hostname与hosts

    参考:http://wp.fungo.me/linux/what-the-hell-is-hostname.html hostname 就是机器名,内核中的一个变量,可临时修改也可以永久修改 /etc ...

  10. MySQL 慢查询日志分析及可视化结果

    MySQL 慢查询日志分析及可视化结果 MySQL 慢查询日志分析 pt-query-digest分析慢查询日志 pt-query-digest --report slow.log 报告最近半个小时的 ...