迭代器的实现

    interface IPeople
    {
        string Name { get; set; }
        int Age { get; set; }
        string Sex { get; set; }
        void Eat();
        void Work();
        void Speak();
    }

    class People : IPeople
    {
       private string name;
       private int age;
       private string sex;


       public People() 
       {
           name = "小王";
           age = 18;
           sex = "";
       }
       public People(string n, int a, string s)
       {
           name = n;
           age = a;
           sex = s;
       }

       public string Name
       {
           get
           {
               return name;
           }
           set
           {
               name = value;
           }
       }

       public int Age
       {
           get
           {
               return age;
           }
           set
           {
               age = value;
           }
       }

       public string Sex
       {
           get
           {
               return sex;
           }
           set
           {
               sex = value;
           }
       }

       public void Eat()
       {
           Console.WriteLine("I'm eating");
       }

       public void Work()
       {
           Console.WriteLine("I'm working");
       }

       public void Speak()
       {
           Console.WriteLine("I'm speaking");
       }


       public System.Collections.IEnumerator GetEnumerator()
       {
           for (int i = 0; i < 3; i++)
           {
               switch (i)
               {
                   case 0:
                       yield return Name;
                       break;
                   case 1:
                       yield return Age;
                       break;
                   case 2:
                       yield return Sex;
                       break;
               }
           }
       }

    }

实现迭代器的常用方法是:在IEnumberator接口上实现GetEnumerator方法

迭代器概述

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

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

  • 迭代器代码使用 yield return 语句依次返回每个元素。yield break 将终止迭代。有关更多信息,请参见 yield

  • 可以在类中实现多个迭代器。每个迭代器都必须像任何类成员一样有唯一的名称,并且可以在 foreach 语句中被客户端代码调用,如下所示:foreach(int x in SampleClass.Iterator2){}

  • 迭代器的返回类型必须为 IEnumerableIEnumeratorIEnumerable<T> 或 IEnumerator<T>。

yield 关键字用于指定返回的值。到达 yield return 语句时,会保存当前位置。下次调用迭代器时将从此位置重新开始执行。

迭代器对集合类特别有用,它提供一种简单的方法来迭代不常用的数据结构(如二进制树)。

 

 

public class DaysOfTheWeek : System.Collections.IEnumerable
{
    string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

    public System.Collections.IEnumerator GetEnumerator()
    {
        for (int i = 0; i < m_Days.Length; i++)
        {
            yield return m_Days[i];
        }
    }
}

class TestDaysOfTheWeek
{
    static void Main()
    {
        // Create an instance of the collection class
        DaysOfTheWeek week = new DaysOfTheWeek();

        // Iterate with foreach
        foreach (string day in week)
        {
            System.Console.Write(day + " ");
        }
    }
}

 

原文地址:https://www.cnblogs.com/anbylau2130/p/2714225.html