您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

.Net 2.0 新功能:迭代器(Iterators)

2024/2/22 22:55:34发布32次查看
什么是迭代器? 迭代器是方法、 get 访问器或运算符,它能使您能够在类或者结构中支持 foreach 迭代,而不必实现整个 ienumerable 接口。 例子: public class yieldtest ... { public static ienumertablepower( int number, int exponent) ... { int count
什么是迭代器?
迭代器是方法、get访问器或运算符,它能使您能够在类或者结构中支持foreach迭代,而不必实现整个ienumerable接口。
例子:
public class yieldtest
...{
public static ienumertable power(int number ,int exponent)
...{
int counter=0;
int result =1;
while(counter++ exponent)
...{
result = result* number
yield return result
}
}
}
1.      迭代器是可以返回相同类型的值的有序序列的一段代码
2.      迭代器可用作方法、运算符或get访问器的代码体
3.      迭代器代码使用yield return语句一次返回每个元素
4.      yield break将终止迭代
5.      可以在类中实现做个迭代器。每个迭代器都必须像任何类成员一样有唯一的名称,并且可以在foreach语句中可调用
6.      迭代器的返回类型必须为ienumerable、ienumberator、ienumerbale或者ienumberator
foreach(int i in yieldtest. power)
{
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product