IEnumerable接口

//     Exposes the enumerator, which supports a simple iteration over a non-generic collection.
public interface IEnumerable
{// Returns an enumerator that iterates through a collection.
IEnumerator GetEnumerator();
}

IEnumerator接口

//     Supports a simple iteration over a nongeneric collection.
public interface IEnumerator
{// Gets the current element in the collection.
object Current { get; }
// Advances the enumerator to the next element of the collection.
bool MoveNext();
// Sets the enumerator to its initial position, which is before the first element in the collection.
void Reset();
}

示例1:

    class MyClass : IEnumerable
{
private int[] sources; public MyClass()
{
this.sources = new int[] { , , , , , , , , , , , , , , , , , , };
} public int this[int index]
{
get { return this.sources[index]; }
} public int Length { get { return this.sources.Length; } } public IEnumerator GetEnumerator()
{
return new MyClassEnumerator(this);
}
} class MyClassEnumerator : IEnumerator
{
private MyClass myobject;
private int index = -; public MyClassEnumerator(MyClass myobject)
{
this.myobject = myobject;
} public object Current
{
get { return this.myobject[index]; }
} public bool MoveNext()
{
if (this.index < this.myobject.Length - )
{
this.index++;
return true;
} return false;
} public void Reset() { this.index = -; }
} static void Main(string[] args)
{
MyClass myobject = new MyClass(); foreach (var item in myobject)
{
Console.WriteLine(item);
} return;
}

示例2: 用yield关键字简化实现.

yield是一个语法糖, 编译器会重新翻译这段代码, 实际上是返回了一个实现了IEnumerator 的对象, 每一次的yield循环对应MoveNext, return this[i]对应于Current属性.

参考:http://www.cnblogs.com/artech/archive/2013/04/14/yield-in-wcf-02.html

    class MyClass : IEnumerable
{
private int[] sources; public MyClass()
{
this.sources = new int[] { , , , , , , , , , , , , , , , , , , };
} public int this[int index]
{
get { return this.sources[index]; }
} public int Length { get { return this.sources.Length; } } public IEnumerator GetEnumerator()
{
//return new MyClassEnumerator(this);
for (int i = ; i < this.Length; i++)
{
yield return this[i];
}
}
}

示例3: 一个对象同时实现了IEnumerable, IEnumerator. 这个实例仅供实验研究, 不推荐实际中应用.

提问: 为什么还要实现IEnumerable, 直接实现了IEnumerator就可以foreach不行吗?

回答: 请把枚举器想象成类似指针的功能, 可以遍历指向的集合.但是一个集合对象最好允许有多个枚举器, 如果集合对象直接实现IEnumerator, 集合对象和枚举器就耦合在一起,一个集合对象就只能有一个枚举器了,也就是它本身就是自己的枚举器。就算是不考虑支持多种枚举器,只考虑对一个枚举器的同时进行多次枚举,只实现IEnumerable也是做不到。

     class MyClass : IEnumerable, IEnumerator
{
private int[] sources;
private int index = -; public MyClass()
{
this.sources = new int[] { , , , , , , , , , , , , , , , , , , };
} public int this[int index]
{
get { return this.sources[index]; }
} public int Length { get { return this.sources.Length; } } public IEnumerator GetEnumerator()
{
//return new MyClassEnumerator(this);
for (int i = ; i < this.Length; i++)
{
yield return this[i];
}
} public object Current
{
get { return this[index]; }
} public bool MoveNext()
{
if (this.index < this.Length - )
{
this.index++;
return true;
} return false;
} public void Reset() { this.index = -; }
} static void Main(string[] args)
{
MyClass myobject = new MyClass(); foreach (var item in myobject)
{
Console.WriteLine(item);
} }

参考:

http://www.cnblogs.com/artech/archive/2013/04/14/yield-in-wcf-02.html

IEnumerable, IEnumerator接口的更多相关文章

  1. C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield

    IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...

  2. 细说 C# 中的 IEnumerable和IEnumerator接口

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  3. 迭代器学习之一:使用IEnumerable和IEnumerator接口

    写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...

  4. foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口

    在.Net中,要想被foreach遍历,那么目标对象要实现IEnumerable或IEnumerable<T>接口,这个接口有一个方法,GetEnumerator(),返回一个IEnume ...

  5. C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)

    前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...

  6. C# IEnumerable 和 IEnumerator接口浅析

    温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆. Foreach常用于循环访问集合,对实现IEnumerable的接口的容器进行遍历,IEnumerable和IEnu ...

  7. IEnumerable和IEnumerator接口

    我们先思考几个问题:1.为什么在foreach中不能修改item的值?(IEnumerator的Current为只读)2.要实现foreach需要满足什么条件?(实现IEnumerator接口来实现的 ...

  8. IEnumerable、IEnumerator接口(如何增加迭代器功能)

    IEnumerable.IEnumerator接口封装了迭代器功能,有了它,我们不需要将内部集合暴露出去,外界只需要访问我的迭代器接口方法即可遍历数据. 在C#中,使用foreach语句来遍历集合.f ...

  9. IEnumerable和IEnumerable<T>接口

    IEnumerable和IEnumerable<T>接口 IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach ...

随机推荐

  1. 为何Redis要比Memcached好用

    Redis是新兴的通用存储系统,而Memcached仍有其适用领域 Memcached还是Redis? 在现代高性能Web应用中这一直是个争论不休的话题. 在基于关系型数据库的Web应用需要提高性能时 ...

  2. scala,import test._ ; import test.{ClassA,ClassB}

    在scala中,*不是通配符,下斜杠“_”才是通配符.因此当使用某个package所有的类时,直接使用:import test._:使用某几个时,直接使用:import test.{ClassA,Cl ...

  3. C++之类成员所占内存大小问题总结

    1.空类所占字节数为1,可见代码如下 #include <iostream> using namespace std; class Parent { }; class Child:publ ...

  4. CentOS 7.2通过yum安装zabbix

    环境说明 系统版本    CentOS 7.2 x86_64 软件版本    yum安装zabbix 3.0.18 首先准备一台纯净的CentOS 7.2系统 1.配置zabbix源 [root@za ...

  5. 【转】javascript 的类,原型,继承的理解

    原文: https://www.cnblogs.com/codernie/p/9098184.html ------------------------------------------------ ...

  6. 理解支持向量机(四)LibSVM工具包的使用

    LibSVM是一款简单易用的支持向量机工具包.包括了C和Java的开发源代码.大家能够訪问其官网进行了解和下载相关文件. 这里以其官网的第一个数据集a1a 为例.练习使用多项式核和径向基核来对数据集进 ...

  7. [React] Override webpack config for create-react-app without ejection

    The default service worker that comes with create-react-app doesn't allow for very much configuratio ...

  8. 资深程序员教你如何实现API自动化测试平台!附项目源码!

    原文链接: 1.平时测试接口,总是现写代码,对测试用例的管理,以及测试报告的管理持久化做的不够, 2.工作中移动端开发和后端开发总是不能并行进行,需要一个mock的依赖来让他们并行开发. 3.同时让自 ...

  9. Java程序猿的JavaScript学习笔记(12——jQuery-扩展选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  10. 51nod 125乘法逆元 (扩展欧几里得)

    给出2个数M和N(M < N),且M与N互质.找出一个数K满足0 < K < N且K * M % N = 1,假设有多个满足条件的.输出最小的. Input 输入2个数M, N中间用 ...