IEnumerable 只有一个方法:IEnumerator GetEnumerator(). INumerable 是集合应该实现的一个接口,这样,就能用 foreach 来遍历这个集合。
IEnumerator 有Current属性,MoveNext(), Reset()两个方法。
 
当 foreach 使用到一个 IEnumerable 的集合上的时候,遍历是这样开始的:
1. 调用 GetEnumerator() 得到一个 IEnumerator 的对象。
2. 调用 MoveNext();
3. 使用 其中一个对象。
4. 重复2和3, 直到 MoveNext()返回 false(没有下一个啦).
 
由此可见
1. GetEnumerator() 返回的对象在最开始的时候,指针是放在第一个对象之前的,Reset()之后也是这样。
2. 为了使用 foreach, 实现 IEnumerable 不是必须的,只是一个 best practice 而已。
 
下面我们来看微软提供的一个例子:

using System;
using System.Collections; // Simple business object.
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
} public string firstName;
public string lastName;
} // Collection of Person objects. This class
// implements IEnumerable so that it can be used
// with ForEach syntax.
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
} // Implementation for the GetEnumerator method.
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
} public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
} // When you implement IEnumerable, you must also implement IEnumerator.
public class PeopleEnum : IEnumerator
{
public Person[] _people; // Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1; public PeopleEnum(Person[] list)
{
_people = list;
} public bool MoveNext()
{
position++;
return (position < _people.Length);
} public void Reset()
{
position = -1;
} object IEnumerator.Current
{
get
{
return Current;
}
} public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
} class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
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); }
} /* This code produces output similar to the following:
*
* John Smith
* Jim Johnson
* Sue Rabon
*
*/

  

IEnumerable & IEnumerator的更多相关文章

  1. C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield

    IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...

  2. IEnumerable, IEnumerator接口

    IEnumerable接口 // Exposes the enumerator, which supports a simple iteration over a non-generic collec ...

  3. ICollection IEnumerable/IEnumerator IDictionaryEnumerator yield

    Enumerable和IEnumerator接口是.NET中非常重要的接口,二者区别: 1. IEnumerable是个声明式的接口,声明实现该接口的类就是“可迭代的enumerable”,但并没用说 ...

  4. c#yield,IEnumerable,IEnumerator

    foreach 在编译成IL后,实际代码如下: 即:foreach实际上是先调用可枚举对象的GetEnumerator方法,得到一个Enumerator对象,然后对Enumerator进行while循 ...

  5. 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)

    [学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...

  6. IEnumerable和IEnumerator

    概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...

  7. IEnumerable和IEnumerator 详解 (转)

    原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...

  8. IEnumerator/IEnumerable接口

    IEnumberator函数成员 Current返回序列中当前位置项的 属性 只读属性 返回object类型 MoveNext把枚举器位置前进到集合中下一项的方法 新位置有效返回true,否则fals ...

  9. 转载IEnumerable与IEnumerator区别

    public interface IEnumerable {     IEnumerator GetEnumerator(); }   public interface IEnumerator {   ...

随机推荐

  1. 配置lnmp

    ===================准备篇: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state –state NEW ...

  2. Swift数组字面量

    可以用一个数组字面量来初始化一个数组,简单地把一个或多个值放在一起就可以了.数组字面量的写法是一行用逗号隔开的值,并在行的两端用一对方括号包起来: [value , value , value ] 下 ...

  3. 用mybatis实现dao的编写或者实现mapper代理

    一.mybatis和hibernate的区别和应用场景hibernate:是一个标准的ORM框架(对象关系映射).入门门槛较高的,不需要写sql,sql语句自动生成了.对sql语句进行优化.修改比较困 ...

  4. 【LeetCode】67. Add Binary

    题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...

  5. C++函数重载实现的原理以及为什么在C++中使用用C语言编译的函数时,要在函数名称前面加上extern "C"声明

    C++相对于C语言而言支持函数重载是其极大的一个特点,相信在使用C语言的时候大家如果要写一个实现两个整型数据相加的函数还要写一个浮点型数据相加的函数,那么这两个函数的名字绝对不可以一样,这样无疑在我们 ...

  6. 普通RAID磁盘数据格式规范

    普通RAID磁盘数据格式规范 1.介绍 在当今的IT环境中,系统管理员希望改变他们正在使用的内部RAID方案,原因可能有以下几个:许多服务器都是附带RAID解决方案的,这些RAID解决方案是通过母板磁 ...

  7. Bootstrap table使用心得---thead与td无法对齐的问题

    当使用工具条中的显示/隐藏列的时候, 经常出现表格的列头与内容无法对齐的问题. 1. 去掉option中的height,完美对齐,但当数据较多的时候,table会自动增加height,显示所有数据而不 ...

  8. haproxy+tomcat集群搭建

    web1和web2的部署可参考我之前的文章<Tomcat集群搭建>,这里就省去该过程了. #安装haproxy- .tar.gz cd haproxy-/ make TARGET=linu ...

  9. gulp实用配置(2)——中小项目

    上一篇的gulp配置很简单,主要就是为了demo的查看和调试,这一篇则会相对详细一些,包括压缩合并打时间戳等. 在互联网环境比较好的城市,需要多人协作的,大一点的项目应该都用上了模块化(这里主要指co ...

  10. Mac之OS系统下搭建JavaEE环境 <二> 之Tomcat 的安装配置

    二.Tomcat的安装与配置 1.下载Tomcat 找到Tomcat的官网 百度搜索Tomcat 点击下载即可 下载网址:http://tomcat.apache.org/download-80.cg ...