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 ...
随机推荐
- Ubuntu 16.04将ISO镜像写入U盘
sudo fdisk -l 查看U盘的路径,如/dev/sdc为U盘的位置,注意位置!注意这个不是挂载的位置. 然后准备好ISO文件,如放在/home/jim/abc.iso 然后输入 sudo dd ...
- NAND Flash Bad Block Table
转:http://wiki.laptop.org/go/NAND_Flash_Bad_Block_Table 1 OLPC NAND Bad Block Management 1.1 Introduc ...
- [置顶]
kubernetes资源类型--secret和Service Account
secret 概念 secret对象类型主要目的是保存和处理敏感信息/私密数据,比如密码,OAuth tokens,ssh keys等信息.将这些信息放在secret对象中比 直接放在pod或dock ...
- c++ comment
一.匈牙利命名法[Hungarian]: 广泛应用于象 Microsoft Windows 这样的环境中. Windows 编程中用到的变量(还包括宏)的命名规则匈牙利命名法,这种命名技术是由一 位能 ...
- android开发常用地址
一. android市场占用率的url http://developer.android.com/about/dashboards/ 二. ADT下载 下载地址是:http://developer.a ...
- Android学习(三) 自动完成的使用
1.AutoCompleteTextView 自动完成功能,在文本框中输入字符,会出现匹配的自动提示.类似百度搜索. XML代码 <?xml version="1.0" en ...
- URL相对路径和URL绝对路径
经常在页面中引用图片,html页面等,自己常常弄错相对路径和绝对路径,今天写下此文总结一下. 直接举例说明吧. 在 D:\例子\html下有这么几个文件和文件夹 1.若引用的资源和本身在 ...
- Android_编程规范与经常使用技巧
一.Android编码规范 1.java代码中不出现中文,最多凝视中能够出现中文 2.局部变量命名.静态成员变量命名 仅仅能包括字母,单词首字母出第一个外.都为大写,其它字母都为小写 3.常量命名 仅 ...
- 基于React的PC网站前端架构分析
代码地址如下:http://www.demodashi.com/demo/12252.html 本文适合对象 有过一定开发经验的初级前端工程师: 有过完整项目的开发经验,不论大小: 对node有所了解 ...
- Java 常见的异常错误分析大集合
算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数组负下标异常:Negative ...