C#编程-94:迭代器Iterator简单实例

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace IEnumerableTest
{
    class Program
    {
        public static IEnumerable GetNextValue()
        {
            yield return "111111";
            yield return "222222";
            yield return "333333";
            yield return "444444";
            yield break;
            yield return "555555";
        }
        static void Main(string[] args)
        {
            Console.WriteLine("===迭代成员===");
            foreach (string item in GetNextValue())
            {
                Console.WriteLine(item);
            }
 
            Console.WriteLine("===迭代类===");
            Months months = new Months();
            foreach (string item in months)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 
    class Months : IEnumerable
    {
        string[] months = { "January","February","March","April","May","June","July","August","September","October","November","December"};
        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < months.Length; i++)
            { 
                yield return months[i];
            }
        }
    }
}

C#编程-94:迭代器Iterator简单实例C#编程-94:迭代器Iterator简单实例C#编程-94:迭代器Iterator简单实例C#编程-94:迭代器Iterator简单实例C#编程-94:迭代器Iterator简单实例C#编程-94:迭代器Iterator简单实例

上一篇:[MySQL FAQ]系列 — 线上环境到底要不要开启query cache


下一篇:《SAP后勤模块实施攻略—SAP在生产、采购、销售、物流中的应用》——1.3 SAP ERP 概览