//迭代器的定义
public class MyCollection:System.Collections.IEnumerable
{
string[] item = {"CCC","AAAA","BBBB","DDDDD" };
public IEnumerator GetEnumerator()
{
for (int i = 0; i < item.Count(); i++)
{
yield return item[i];
}
}
}
//应用
MyCollection mc = new MyCollection();
foreach (var v in mc)
{
MessageBox.Show(v.ToString());
}
//继承System.Collections.IEnumerable编译器回自动添加
//moveNext()、reset()、current()、等
//迭代器的应用在于foreach,在编译器层面上是个语法糖,最终编译的结果是
do while(item.movenext())
{
.......
}