C#接口之IEnumerable,IEnumerator
IEnumerable

截图来源于https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerable.getenumerator.aspx
IEnumerable只包含一个抽象方法GetEnumerable(),它返回一个可用于循环访问集合的IEnumberator对象。我们可以通过定义foreach语句功能实现并支持非泛型方法的迭代。
在MSDN上给出了一个关于GetEnumerable()方法的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace TestInterface
{ public class Person
{
public Person(string fName,string lName)
{
this.firstName = fName;
this.lastName = lName;
} public string firstName;
public string lastName;
} public class People:IEnumerable//继承IEnumerable类,重写GetNumberable()方法
{
private Person[] _peopele;
public People(Person[] pArray)
{
_peopele = new Person[pArray.Length];
for (int i = ; i < pArray.Length; i++)
_peopele[i] = pArray[i];
} IEnumerator IEnumerable.GetEnumerator()//实现GetEnumerable接口
{
return (IEnumerator) GetEnumerator();//调用重写的GetEnumerator方法
} public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_peopele);//返回一个PeopleEnum类型的对象,PeopleEnum继承了IEnumerator类
}
} public class PeopleEnum:IEnumerator
{
public Person[] _people;
int position = -;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="list">
/// _people指针指向list数组
/// </param>
public PeopleEnum(Person[] list)
{
_people = list;
} /// <summary>
///将枚举数推进到集合的下一个元素。 /// </summary>
/// <returns>
/// 成功返回true,失败返回false
/// </returns>
public bool MoveNext()
{
position++;
return (position < _people.Length);
} /// <summary>
/// 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
/// </summary>
public void Reset()
{
position = -;
} /// <summary>
/// 获取当前位置的对象
/// </summary>
object IEnumerator.Current
{
get { return Current; }
} /// <summary>
/// 实现接口方法
/// </summary>
public Person Current
{
get
{
try
{
return _people[position];
}
catch(IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
} class Program
{ static void Main(string[] args)
{
Person[] peopleArray = new Person[]
{
new Person("John","Smith"),
new Person("Jim","Johnson"),
new Person("Sue","Rabon"),
};
People peopleList = new People(peopleArray);//初始化对象集合
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName); }
}
}
如果我们将第23-42代码改写如下:
public class People//继承IEnumerable类,重写GetNumberable()方法
{
private Person[] _peopele;
public People(Person[] pArray)
{
_peopele = new Person[pArray.Length];
for (int i = ; i < pArray.Length; i++)
_peopele[i] = pArray[i];
}
}
即不让People继承IEnumerable类,重写GetNumerator接口的部分也删除掉。那么编译器就会提示:

这是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,以至于foreach语句不能遍历Person集合。
IEnumerator
下面来介绍一下IEnumerator

截图来源于:https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerator(v=vs.110).aspx
属性 描述
Current 获取集合中的当前元素
方法 描述
MoveNext 将枚举数推进到集合的下一个元素。
Reset 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
正是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,那么也就不能使用MoveNext方法,foreach也就不能实现遍历Person集合。
C#接口之IEnumerable,IEnumerator的更多相关文章
- IEnumerable, IEnumerator接口
IEnumerable接口 // Exposes the enumerator, which supports a simple iteration over a non-generic collec ...
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- C# 常用接口学习 IEnumerable<T>
作者:乌龙哈里 时间:2015-10-24 平台:Window7 64bit,Visual Studio Community 2015 本文参考: MSDN IEnumerable<T> ...
- ICollection IEnumerable/IEnumerator IDictionaryEnumerator yield
Enumerable和IEnumerator接口是.NET中非常重要的接口,二者区别: 1. IEnumerable是个声明式的接口,声明实现该接口的类就是“可迭代的enumerable”,但并没用说 ...
- c#yield,IEnumerable,IEnumerator
foreach 在编译成IL后,实际代码如下: 即:foreach实际上是先调用可枚举对象的GetEnumerator方法,得到一个Enumerator对象,然后对Enumerator进行while循 ...
- IEnumerable & IEnumerator
IEnumerable 只有一个方法:IEnumerator GetEnumerator(). INumerable 是集合应该实现的一个接口,这样,就能用 foreach 来遍历这个集合. IEnu ...
- C#内建接口:IEnumerable
这节讲一下接口IEnumerable. 01 什么是Enumerable 在一些返回集合数据的接口中,我们经常能看到IEnumerable接口的身影.那什么是Enumerable呢?首先它跟C#中的e ...
- 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)
[学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- [设计模式] Iterator - 迭代器模式:由一份奥利奥早餐联想到的设计模式
Iterator - 迭代器模式 目录 前言 回顾 UML 类图 代码分析 抽象的 UML 类图 思考 前言 这是一包奥利奥(数组),里面藏了很多块奥利奥饼干(数组中的元素),我将它们放在一个碟子上慢 ...
随机推荐
- 安装ELK
1. 安装Elasticsearch a. 下载 : https://download.elasticsearch.org/elasticsearch/release/org/elasticsearc ...
- Linux下的tree命令 --Linux下文件夹树查看
Linux下的tree命令 --Linux下文件夹树查看 有时我们须要生成文件夹树结构,能够使用的有ls -R,可是实际效果并不好 这时须要用到tree命令,可是大部分Linux系统是默认不安装该命令 ...
- JavaScript——DOM或以树形展示的Web页面
Web网页的一般能够通过document以及document所相关的各种元素组成.当然我们也能够通过层次结构的树形结构在展现Web页面.假设要对一个网页进行改动的话,我们能够通过document对象. ...
- linux io architecture
http://www.cs.columbia.edu/~krj/os/lectures/L24-IO.pdf http://events.linuxfoundation.org/sites/event ...
- 深入理解php 匿名函数和 Closure
而在PHP 5.3发布的时候, 其中有一条new feature就是支持闭包/Lambda Function, 我第一反应是以为zval新增了一个IS_FUNCTION, 但实际上是构造了一个PHP ...
- 再访贺利坚(一):IT毕业生去培训机构,这件事很正常(转载)
转载自: 再访贺利坚(一):IT毕业生去培训机构,这件事很正常 导语:与烟台大学计算机学院贺利坚副教授相识,还是在2012年年底,那个时候我在为社区之星专访栏目寻找合适的采访人.在社区运营的推荐下,我 ...
- ld,连接器
连接器的功能,是将一个可执行程序所需的目标文件和库文件最终整合为一体.一个程序通常包含传统的三个段,.test, .data, .bss段.连接器的功能就是将各个目标文件个库文件中的三个段进行合并. ...
- python学习笔记(18)--eclipse更换主题
说明: 1. 在eclipse marketplace 搜索color ide pack安装 2. 参考文章和评论http://blog.csdn.net/wusuopubupt/article/de ...
- JAVA培训资料
JAVA培训资料 一.Java语言 1.面向对象的三个基本特征 2.方法重载和方法重写的概念和区别 3.接口和内部类.抽象类的特性 4.文件读写的基本类 **5.串行化的注意事项以及如何实现串行化 6 ...
- 一款纯css3实现的图片3D翻转幻灯片
之前介绍了好多款网页幻灯片,今天要给大家再带来一款纯css3实现的图片3D翻转幻灯片.这款幻灯片图片轮播采用了3D翻转的形式,效果非常不错.一起看下效果图: 在线预览 源码下载 实现的代码. ht ...