IEnumerable, IEnumerator接口
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接口的更多相关文章
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- 细说 C# 中的 IEnumerable和IEnumerator接口
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- 迭代器学习之一:使用IEnumerable和IEnumerator接口
写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...
- foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口
在.Net中,要想被foreach遍历,那么目标对象要实现IEnumerable或IEnumerable<T>接口,这个接口有一个方法,GetEnumerator(),返回一个IEnume ...
- C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)
前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...
- C# IEnumerable 和 IEnumerator接口浅析
温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆. Foreach常用于循环访问集合,对实现IEnumerable的接口的容器进行遍历,IEnumerable和IEnu ...
- IEnumerable和IEnumerator接口
我们先思考几个问题:1.为什么在foreach中不能修改item的值?(IEnumerator的Current为只读)2.要实现foreach需要满足什么条件?(实现IEnumerator接口来实现的 ...
- IEnumerable、IEnumerator接口(如何增加迭代器功能)
IEnumerable.IEnumerator接口封装了迭代器功能,有了它,我们不需要将内部集合暴露出去,外界只需要访问我的迭代器接口方法即可遍历数据. 在C#中,使用foreach语句来遍历集合.f ...
- IEnumerable和IEnumerable<T>接口
IEnumerable和IEnumerable<T>接口 IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach ...
随机推荐
- 用Xcode6的Leaks检测内存泄漏
用xcode打开项目之后,选择Product - Profile: 在弹出的窗口中选择Leaks: 然后在设备解锁的情况下,选择Leaks再点击左上角的红色按钮开始运行APP: 红色的柱子表示有内存泄 ...
- easyui combogrid 按需加载,点击下拉加载
功能优点:减少不必要的http请求,减少服务器查询压力,降低额外的浏览器渲染,提高呈现速度开发分享: combogrid 点击才请求的功能实现简要:我分析了费用系统,和现在全网的写法.并不满意.都是要 ...
- 正规化方程Normal Equations解析
如果需要代做算法,可以联系我...博客右侧有联系方式. 一.正规化方程概念 假设我们有m个样本.特征向量的维度为n.因此,可知样本为{(x(1),y(1)), (x(2),y(2)),... ..., ...
- linux mysql cluser集群
管理节点的安装与启动 config.init内容如下 [NDBD DEFAULT] NoOfReplicas=1 #定义在Cluster环境中相同数据的份数,最大为4 [NDB_MGMD] #设置管理 ...
- sharepoint2010无法连接到配置数据库。
最近VS部署又遇到这个问题了,记录一下. 然后以管理员省份打开sharePoint管理中心,发现页面展示光秃秃的“无法连接到配置数据库” 解决方式:问的同事. 找到计算机 服务 项, 这个启动下 这个 ...
- php设计模式之解释器模式
解释器设计模式用于分析一个实体的关键元素,并且针对每个元素都提供自己的解释或相应的动作. <?php /** * 解释器模式 */ class User { protected $_userna ...
- 面试题:Add Two Numbers(模拟单链表)
题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- ES里关于对象的拓展
一.对象类别 在浏览器这样的执行环境中,对象没有统一的标准,在标准中又使用不同的术语描述对象,ES6规范清晰定义了每一个类别的对象,对象的类别如下 1.普通(Ordinary)对象:具有JS对象所有的 ...
- tensorflow dynamic rnn源码分析
python3.6,tensorflow1.11 测试代码: tensorflow在eager模式下进行测试,方便调试,查看中间结果 import tensorflow as tf tf.enable ...
- 基于Redis的Bloomfilter去重(转载)
转载:http://blog.csdn.net/bone_ace/article/details/53107018 前言 “去重”是日常工作中会经常用到的一项技能,在爬虫领域更是常用,并且规模一般都比 ...