- 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简单实例
最新推荐文章于 2018-11-30 23:57:20 发布