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. 3.非标准的NDEF格式数据解析--IsoDep

    1.使用目的:正常开发是针对NDEF格式数据进行开发,但实际情况并非如此,以厦门公交卡为例,厦门公交卡保存的是非NDEF格式数据.其类型是IsoDep类型. 2.非标准的NDEF格式数据流程:当厦门公 ...

  2. linux命令详解:df命令

    转:http://www.cnblogs.com/lwgdream/p/3413579.html 前言 df命令用来查看系统的space和inode使用情况,也是常用命令之一 使用说明 -a 显示所有 ...

  3. EXISTS语句的子查询

    一.EXISTS运算符简介: 使用EXISTS语句可以测试集合是否为空,EXISTS语句通常与子查询结合在一起使用.只要子查询中至少返回一个值,则EXISTS语句的值就为True.EXISTS子查询的 ...

  4. DotnetBrowser高级教程-(5)使用内置的MVC UI框架-EasyMvc

    如果DotnetBrowser只是实现了内置chrome浏览器和web/web socket server,似乎还不是很完美.因此,最新的DotnetBrowser已经内置对easy mvc控件的支持 ...

  5. apache的order allow deny

    这个东西确实挺容易让我们迷糊.其实也不难,只要你掌握这样一条规律即可:首先举个例子: Order deny,allowdeny  from allallow from 127.0.0.1 我们判断的依 ...

  6. Ubuntu16.04下安装googlechrome flash 插件和安装网易云音乐

    一.ubuntu 16.04 下安装完后发现 flash无法播放没有安装flash插件因为 Adobe Flash 不再支持 linux Google 便开发了PepperFlashPlayer来替代 ...

  7. ieda常用快捷键

    Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shift+Click,可以关闭文件Ctrl+[ ...

  8. Linux取消挂载,删除用户及其目录

    取消挂载 取消挂载命令: umount /dev/sdb 命令umount 文件系统/挂载点 umount /dev/sdb 例如:umount /dev/sdb即可将sdb1取消挂载. 如果出现de ...

  9. CKEditor+SWFUpload实现功能较为强大的编辑器(三)---后台接收图片流程

    在前台配置完CKEditor和SWFUpload之后就可以满足基本的需求了 在这里,我配置的接收异步上传的图片的页面为upload.ashx 在这个ashx中对上传的图片处理的流程如下: contex ...

  10. 转:大数据 2016 landscape

    如图: