[翻译+补充说明]
原文参考:http://www.theserverside.net/articles/showarticle.tss?id=IteratorsWithC2
注释:对于Enumerable和Enumerator的翻译可能不太贴切
可以参考以下词汇:枚举 枚举构造器
介绍
迭代器作为匿名方法存在是很不明显的,但是其强大的特性现在被引入到Csharp2.0版本中了。迭代器有资格作为语法构造的一部分出现并不意味着从.NET 1.x过渡到2.0版本后通用语言运行时与.NET中间语言指令集发生了改变。只是如同匿名方法一样,所有的这些魔法都隐藏到编译器中了。为了理解下面的CSharp2.0中迭代器的原理,我们将深入到其内部中,并适当的运用一些在2.0版本中出现的基本特性,接着我们还将分析其中编译器的内部工作机制,最后我们还将掌握在CSharp2.0使用迭代器的一些高级技术。
Csharp1.x中迭代器,枚举,枚举器以及使用迭代设计模式
在我们分析CSharp1和CSharp2的枚举器之前。我们有必要知道什么是可枚举的以及什么是枚举器。我们在谈及一个对象是可枚举的是指如果一个对象包含或引用了一个对象集和如果一个元素属于这个集合那么我们能够使用CSsharp关键字’foreach’列举出来。实现了System.Collections.IEnumerable接口的实例化的类都是可枚举的。下面我们将会看到,具有某些可枚举特征并不意味着一定满足这个条件。
{
//为接口绑定属性
[System.Runtime.InteropServices.GuidAttribute(“496B0ABE-CDEE-11d3-88E8-00902754C43A”)]
//注意这里是为了运行时反射
//我们看看该配件的自描述
//使用了GuidAttribute类,这里我使用了Reflector查看了该类的实现
//该类是这样描述的
//[AttributeUsage(AttributeTargets.Delegate | (AttributeTargets.Interface | (AttributeTargets.Enum | (AttributeTargets.Struct | (AttributeTargets.Class | AttributeTargets.Assembly)))), Inherited=false)]
//ArrtibuteTargets是枚举类型
//指出该类绑定了代理,接口,枚举,结构,类以及配件,并且不可继承
//这里的继承含义不是类的派生,而是指出属性不可继承,这意味假设存在派生类并对其类重新进行了属性绑定,那么派生类所绑定的属性将覆盖基类所绑定的属性
//不过,该类同时也是一个不可继承的类,这在类的定义中可以看到含有一个sealed修饰符
public interface IEnumerable
{
[System.Runtime.InteropService.DispIdAttribute(252)]
System.Collections.IEumerator GetEnumerator();
}
//为接口绑定属性
[System.Runtime.InteropServices.GuidAttribute("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEumerator
{
//返回当前集合中的元素
object Current
{
get;
}
//返回值true表示迭代器成功前进到集合中的下一个元素
//返回值false表示已经位于集合的末尾
bool MoveNext();
//恢复初始化指向的位置
void Reset();
}
}
客户端调用IEnumerable.GetEnumerator()方法实际上是通过IEnumerator的Current属性,该属性在派生类EnumeratorConcret实现,并实例化为EnumeratorConcret。当MoveNext()和Reset()方法被调用的时候,枚举器将保持当前的索引,这样客户端就可以通过Current属性访问集合中的元素。它的UML视图看起来像这样:
对于那些知晓GoF设计模式的人来说,CSharp迭代器的实现是显而易见的,这个设计模式被命名为迭代;
一个例子
using System.Collections;
public class Persons : IEnumerable
{
private class PersonsEnumerator : IEnumerator
{
private int index = -1;
private Persons P;
public PersonsEnumerator(Persons P)
{
this.P = P;
}
public bool MoveNext()
{
index++;
return P.m_Names.Length;
}
public void Reset()
{
index = -1;
}
public object Current
{
get
{
return P.m_Names[index];
}
}
}
string[] m_Names;
public Persons(params string[] Names)
{
m_Names = new string[Names.Length];
Names.CopyTo(m_Names,0);
}
private string this[int index]
{
get
{
return m_Names[index];
}
set
{
m_Names[index] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Persons arrPersons = new Persons(“Michel”,”Christine”,”Mathieu”,”Julien”);
foreach (string s in arrPersons)
{
Console.WriteLine(s);
Console.ReadLine();
}
}
}
该程序将输出:
Michel
Christine
Mathieu
Julien
这段代码中的大部分语句用来实现枚举器的索引行为,我们更希望看到CSharp2.0达到同样功能却戏剧性的缩小的代码数量。
注意:C#编译器类似如下代码来解释foreach关键字
{
static void Main(string[] args)
{
Persons arrPersons = new Persons(“Michel”,”Christine”,”Mathieu”,”Julien”);
IEnumerator e = arrPersons.GetEnumerator();
While (e.MoveNext())
{
Console.WriteLine((string)e.Current);
Console.ReadLine();
}
}
}
单一枚举包含多个枚举构造器
一个可枚举类不一定要服从实现IEnumerable接口。这个责任可以被委托到其他类中实现。例如:
using System.Collections;
public class Persons //这里没有实现IEnumerable接口
{
private class PersonsEnumerator :IEnumerator
{
…
}
private class PersonsEnumerable :IEnumerable
{
private Persons m_Persons;
inernal PersonEnumerable(Persons persons)
{
m_Persons = persons;
}
IEnumerator IEnumerable.GetEnumerator()
{
return new PersonsEnumerator(m_Persons);
}
}
public IEnumerable InOrder
{
get
{
return new PersonsEnumerable(this);
}
}
}
class Program
{
static void Main(string[] args)
{
Persons arrPersons = new Persons(“Michel”,”Christine”,”Mathieu”,”Julien”);
foreach (string s in arrPersons.InOrder)
{
Console.WriteLine(s);
Console.ReadLine();
}
}
}
这个方法非常方便的在同一个可枚举类中实现了多个枚举构造器类。一个反序的枚举器(元素字段以反序排列)或一个具有EvenPosOnly字段的用于连续排列元素的枚举器会更加有用。
CSharp 1.0迭代器的缺点
未完待续