2012-04-29 00:44:12|?次阅读|上传:wustguangh【已有?条评论】发表评论
在上面People类就实现了IEnumerable接口(当然截至到目前其相关内容也必须实现IEnumerator类),因此我们就可以遍历People类:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorDemo
{
class Program
{
/*
* 实现IEnumerable(显然目前我们必须先实现IEnumerator)
*/
static void Main(string[] args)
{
Person[] persons = new Person[] {
new Person("Kenshin","Cui"),
new Person("Miaoer","Sun"),
new Person("Jinjuan","Shen"),
new Person("Yanxin","Nie")
};
People pe = new People(persons);
foreach (Person p in pe)
{
Console.WriteLine(p.firstName + " " + p.lastName);
}
Console.Read();
}
}
}
截止到现在我们可以看到如果让一个类或结构支持foreach就必须实现整个IEnumerable接口,这显然过于麻烦,毕竟我们不想在这方 面花费太多的时间,那么此时我们就来使用迭代器吧。创建迭代器的最常用的方法就是对IEnumerable接口实现GetEnumerator()方法, 例如将上面的People类可以写成这样:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorDemo
{
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];
}
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < _people.Length; ++i)
{
yield return _people[i];
}
}
}
}
从上面我们可以看到我们完全省略了创建PeopleEnum的过程(事实上我们还可以更简单,下面我们就可以看到,这里主要为了和上面的例子做 个对比),当然这一切都归功于迭代器的功劳。迭代器使用yield return语句返回每个元素,yield break终止迭代(其返回类型必须为IEnumerable/IEnumerable、IEnumerator/Ienumerator类型)。 yield关键字用于指定返回值,到达yield break时会保存当前位置,直到下次调用迭代器时将从此位置从新开始执行。当编译器见到迭代器时,会自动生成 IEnumerable/IEnumerable接口的Current、MoveNext和Dispose方法。
当然可能有朋友到现在还有些模糊,那么您不妨简单的理解为:迭代器就是使用yield帮助我们省去了实现IEnumerator的麻烦(虽然,事实上远不止那么简单)。
之所以今天会想起这个话题,其实是因为偶然看到一段类似下面代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorDemo
{
class Program
{
private static IList<Person> FilterPeople(IEnumerable<Person> people)
{
IList<Person> persons = new List<Person>();
foreach (Person p in people)
{
if (p.lastName == "Cui")
{
persons.Add(p);
}
}
return persons;
}
static void Main(string[] args)
{
Person[] persons = new Person[] {
new Person("Kenshin","Cui"),
new Person("Miaoer","Sun"),
new Person("Jinjuan","Shen"),
new Person("Yanxin","Nie")
};
foreach(Person p in FilterPeople(persons))
{
Console.WriteLine(p.firstName + " " + p.lastName);
}
Console.Read();
}
}
}
想象一下如果使用迭代块(包含yield语句的方法或属性)会不会更优雅呢:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorDemo
{
class Program
{
/*
* 使用迭代快简化类似的方法
*/
private static IEnumerable<Person> FilterPeople(IEnumerable<Person> people)
{
foreach(Person p in people)
{
if(p.lastName=="Cui")
{
yield return p;
}
}
}
static void Main(string[] args)
{
Person[] persons = new Person[] {
new Person("Kenshin","Cui"),
new Person("Miaoer","Sun"),
new Person("Jinjuan","Shen"),
new Person("Yanxin","Nie")
};
foreach(Person p in FilterPeople(persons))
{
Console.WriteLine(p.firstName + " " + p.lastName);
}
Console.Read();
}
}
}