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

WBOY
リリース: 2016-06-07 15:19:57
オリジナル
1677 人が閲覧しました

什么是迭代器? 迭代器是方法、 get 访问器或运算符,它能使您能够在类或者结构中支持 foreach 迭代,而不必实现整个 IEnumerable 接口。 例子: public class YieldTest ... { public static IEnumertablePower( int number, int exponent) ... { int count

什么是迭代器?

迭代器是方法、get访问器或运算符,它能使您能够在类或者结构中支持foreach迭代,而不必实现整个IEnumerable接口。

例子:

.Net 2.0 新功能:迭代器(Iterators)public class YieldTest
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators).Net 2.0 新功能:迭代器(Iterators)
...{
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators)   
public static IEnumertable Power(int number ,int exponent)
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators).Net 2.0 新功能:迭代器(Iterators)
...{
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators)   
int counter=0;
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators)   
int result =1;
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators)   
while(counter++ exponent)
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators).Net 2.0 新功能:迭代器(Iterators)
...{
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators)  result 
= result* number
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators)  
yield return result
.Net 2.0 新功能:迭代器(Iterators)
.Net 2.0 新功能:迭代器(Iterators)}

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

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

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


 

1.      迭代器是可以返回相同类型的值的有序序列的一段代码

2.      迭代器可用作方法、运算符或get访问器的代码体

3.      迭代器代码使用yield return语句一次返回每个元素

4.      yield break将终止迭代

5.      可以在类中实现做个迭代器。每个迭代器都必须像任何类成员一样有唯一的名称,并且可以在foreach语句中可调用

6.      迭代器的返回类型必须为IEnumerableIEnumberatorIEnumerbale或者IEnumberator

foreach(int i in YieldTest. Power)

{

 

}

 
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!