答:实现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. 百度地图API简单应用

    在做移动端应用时经常用到百度地图API,百度API有强大的示例和文档,开发之前去百度相关网站注册密钥,很块博主只花了几分钟 百度地图API范例 百度地图API文档说明 例子1:输入特定关键字绘制地图标 ...

  2. 带有“非简单参数”的函数为什么不能包含 "use strict" 指令

    非简单参数就是 ES6 里新加的参数语法,包括:1.默认参数值.2.剩余参数.3.参数解构.本文接下来要讲的就是 ES7 为什么禁止在使用了非简单参数的函数里使用 "use strict&q ...

  3. hadoop源码编译——2.5.0版本

    强迫症必治: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using b ...

  4. [Centos] Centos 7笔记

    命令行与图形界面 在X-Window图形操作界面中按“Alt+Ctrl+功能键Fnn=1~6”就可以进入Console字符操作界面.这就意味着你可以同时拥有X-Window加上6个Console字 ...

  5. $this-->name

    如果要在模板中输出变量,必须在在控制器中把变量传递给模板,系统提供了assign方法对模板变量赋值,无论何种变量类型都统一使用assign赋值. $this->assign('name',$va ...

  6. I18N 国际化

    http://blog.sina.com.cn/s/blog_6c7e59770101p7w9.html 一.I18N 在 J2EE 中的应用 [转载:http://blog.csdn.net/cha ...

  7. 【Python基础学习五】列表,元祖,字典

    1.列表(list) 列表是Python的一种内置数据类型,list是一种有序的集合,可以随时添加和删除其中的元素,就像动态数组一样.获取list中的元素用角标获取,角标可以使用正角标,也可以使用负角 ...

  8. 前端小知识(转载http://www.cnblogs.com/Wayou/p/things_you_dont_know_about_frontend.html)

    前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...

  9. appCan uexLocation 定位功能

    js的引用: <script src="../js/zy_control.js"></script> <script src="../js/ ...

  10. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...