答:实现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. TransactionScope 之分布式配置

    .Net开发过程中,涉及多个数据库和不同数据库的分布式事务(Distributed Transaction)开发,有时会碰到“与基础事务管理器的通信失败”的错误.导致这个错误一般有下列三个原因:1)  ...

  2. 【译文】 GC 安全点 和安全区域

    原文链接 : here 根引用  Root references  一个实例死了,意味着它变得无用.只用程序员知道一个实例是否已经无用.为了让程序知道一个实例是否已经无用,我们可以使用编译器分析,引用 ...

  3. 1、Jsp页面

    一.JSP(java server page):是以Java语言为基础的动态网页生成技术. 1.特点: a).以 .jsp 为后缀的文本文件,不需要编译(相对于程序猿来说不需要编译) b).以html ...

  4. UIScrollView和delegate的通信

    在OC中,发送消息的意思就是调用方法 因此UIScrollView和delegate的通信可以理解为下图所示 再精确一点,UIScrollView和delegate的通信应该为下图所示 可以看出,要想 ...

  5. 【C语言入门教程】目录/大纲

    第一章 C语言编程基础 1.1 基本程序结构 1.2 函数库 和 链接 1.3 C语言“32个”关键字 第二章 数据类型.运算符和表达式 2.1 数据类型(5种基本数据类型),聚合类型与修饰符 2.2 ...

  6. Python里*arg 和**kwargs的作用

    Hi,伙计们.我发现Python新手们在理解*args和**kwargs这两个魔法变量时都有些困难.他们到底是什么?首先,我先告诉大家一个事实,完整地写*args和**kwargs是不必要的,我们可以 ...

  7. ORA-27101 ORACLE not available

    问题描述:今天打开plsql,报错无法识别连接服务 1.然后去找,是不是oracle服务没有启动,但是发现oracle的其他服务都能启动.只有OracleDBConsolearies不能启动,这说明O ...

  8. CPU时间片

    CPU时间片 为了提高程序执行效率,大家在很多应用中都采用了多线程模式,这样可以将原来的序列化执行变为并行执行,任务的分解以及并行执行能够极大地提高程序的运行效率. 但这都是代码级别的表现,而硬件是如 ...

  9. css的用法

    Css(Cascading Style Sheets,层叠样式表)是一种页面美化方法,通过编辑Css的对象属性达到美化页面的效果.Css的操作基本单元为对象,使用CSS的感觉就像是使用C++/C中的函 ...

  10. request \response 总结

    request&response request 1.获得信息的方法     1> 获得请求首行信息的方法         *getMethod         *getContextP ...