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 类图 思考 前言 这是一包奥利奥(数组),里面藏了很多块奥利奥饼干(数组中的元素),我将它们放在一个碟子上慢 ...
随机推荐
- 权限管理系统(一):权限系统与RBAC模型概述
RBAC模型概述 RBAC即角色访问控制(Role Based Access Control) RBAC认为权限授权实际上是Who.What.How的问题.在RBAC模型中,who.what.how构 ...
- Oracle学习笔记之六(DDL:表、索引、视图、同义词、序列操作相关SQL)
下面这些基本的SQL语句应该熟悉,能够灵活运用.最好在不查资料的情况下,能够写出如下的任何代码. 1. 数据表操作相关 --创建表 create table STUDENTS( STUNO ) not ...
- Bash Shell启动配置脚本的顺序
1.Bash检查环境变量文件的方式,取决于系统运行Shell的方式,通常系统运行Shell有3种方式: )通过系统用户登陆后默认运行的Shell )非登陆交互式运行Shell )执行脚本运行非交互式S ...
- 搭建MAC下vim环境
MAC下的IDE实在是不好用,最终放弃了IDE准备直接用vim来看代码了,那么就需要设置一下vim. 将家目录下面.vim中的vimrc用下面的内容替换: " Set vundle sett ...
- ny49 开心的小明
开心的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他 ...
- js中作用域和闭包
作用域链实例 (1) function example() { var age = 23; alert(age) } var age = 25; example(); alert(age); // ...
- tomcat 部署时修改服务器时间
tomcat 在部署时修改了服务器时间 会出现以下状况 1.session 失效 2.修改的文件不会正确被tomcat热部署进去
- poj2018(高精度二分+dp)
题意:给你n个数,要你在这n个数里面找到一些连续的数,这些数的数量大于等于m,并且他们的平均值在这n个数里面是最大的....... 思路:先把n个数的最大最小值确定,然后二分枚举平均值,对于每一个连续 ...
- 一款纯css3实现的动画加载导航
之前为大家介绍了好几款导航菜单,今天为给大家再带来一款纯css3实现的动画加载导航.该导航出现的时候以动画的形式出现.效果图如下: 在线预览 源码下载 实现的代码. html代码: <ul ...
- C语言 · 龟兔赛跑预测
基础练习 龟兔赛跑预测 时间限制:1.0s 内存限制:512.0MB 锦囊1 模拟. 问题描述 话说这个世界上有各种各样的兔子和乌龟,但是研究发现,所有的兔子和乌龟都有一个共 ...