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 类图 思考 前言 这是一包奥利奥(数组),里面藏了很多块奥利奥饼干(数组中的元素),我将它们放在一个碟子上慢 ...
随机推荐
- struts中action名称反复导致的神秘事件
近期由于项目需求变更.须要本人对当中的某个业务功能进行改动.本人依照前台页面找action,依据action找代码的逻辑进行了改动(公司项目是ssh框架,struts配置全部是通过注解的方式进行.配置 ...
- MongoDB add sharding -- Just a note
1. Configure Configuration Server. 1.1. Create a directory: e.g. C:\data\dbs\config 1.2. Start confi ...
- php分页函数示例代码
分享一例php分页函数代码,用此函数实现分页代码很不错. 代码,php分页函数. <?php /* * Created on 2011-07-28 * Author : LKK , http:/ ...
- copy 的实现原理与深浅拷贝
转自:http://bbs.9ria.com/thread-210322-1-1.html 首先,从copy开始说,简而言之,copy的目的就是生成一个新的实例,然后把其成员都按原实例赋值.对于非指针 ...
- Rewrite MSIL Code on the Fly with the .NET Framework Profiling API
http://clrprofiler.codeplex.com/ http://blogs.msdn.com/b/davbr/archive/2012/11/19/clrprofiler-4-5-re ...
- ny16 矩形嵌套
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a< ...
- 《SPARK/TACHYON:基于内存的分布式存储系统》-史鸣飞(英特尔亚太研发有限公司大数据软件部工程师)
史鸣飞:大家好,我是叫史鸣飞,来自英特尔公司,接下来我向大家介绍一下Tachyon.我事先想了解一下大家有没有听说过Tachyon,或者是对Tachyon有没有一些了解?对Spark呢? 首先做一个介 ...
- maven打包可以行文件,包含依赖包等
<build> <!-- 设定打包的名称 --> <finalName>ismp2xy</finalName> <plugins> < ...
- java多线程13 : 死锁
前言 死锁单独写一篇文章是因为这是一个很严重的.必须要引起重视的问题.这不是夸大死锁的风险,尽管锁被持有的时间通常很短,但是作为商业产品的应用程序每天可能要执行数十亿次获取锁->释放锁的操作,只 ...
- 3dMax常见问题
误关掉了命令面板顶部菜单栏[自定义]->[显示UI]->勾选上命令面板 调整右侧命令面板的位置鼠标放在命令面单顶部,当显示两个长方形时,可以鼠标左键拖动. 命令面板拖动后可以停 ...