初探迭代器

代码
using System;
using System.Collections;
using System.IO;
using System.Threading;
using System.Collections.Generic;

//还是有很多不明白的
namespace test {
public class Persons {
public IEnumerable<IEnumerable<int>> a(int i1,int ii) {//返回类型为IEnumerable<int>迭代器名字为a包含2个参数(不知道为什么不用参数会发生错误)
List<int> list = new List<int>();//新建一个list<int>
for (var i = i1; i != ii; i++) {//控制迭代的次数
list.Add(i);//加入到list
yield return list;//返回结果
}
}
}
class program {
static void Main() {
Persons arrPersons
= new Persons();
foreach (var s in arrPersons.a(0,10)) {
foreach (var i in s) {
Console.Write(i);
}
Console.WriteLine();
}
System.Console.ReadLine();
}
}
}

 

posted on 2010-11-24 15:46  卑鄙De小贝  阅读(216)  评论(0)    收藏  举报

导航