原文地址:http://msdn.microsoft.com/zh-cn/library/65zzykke.aspx
创建迭代器最常用的方法是对 IEnumerable 接口实现 GetEnumerator 方法,例如:
public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}
foreach,如下所示:
static void Main()
{
ListClass listClass1 = new ListClass();
foreach (int i in listClass1)
{
System.Console.Write(i + " ");
}
// Output: 0 1 2 3 4 5 6 7 8 9
}