zoukankan      html  css  js  c++  java
  • .Net 2.0 新功能:迭代器(Iterators)

    什么是迭代器?

    迭代器是方法、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.      迭代器的返回类型必须为IEnumerableIEnumberatorIEnumerbale<T>或者IEnumberator<T>

    foreach(int i in YieldTest. Power)

    {

     

    }

     
  • 相关阅读:
    SD卡测试
    测试人员可能会遇到的问题
    HDU 1024 Max Sum Plus Plus
    HDU 1176 免费馅饼
    HDU 1257 最少拦截系统
    HDU 1087 Super Jumping! Jumping! Jumping!
    poj 1328 Radar Installation
    poj 1753 Flip Game
    HDU 1003 Max Sum
    HDU 5592 ZYB's Premutation(BestCoder Round #65 C)
  • 原文地址:https://www.cnblogs.com/hainange/p/6153348.html
Copyright © 2011-2022 走看看