using System.Collections;//必须添加的命名空间

namespace 迭代的一般应用
{
    class Program
    {
        static void Main(string[] args)
        {
            IterationMonths im = new IterationMonths();

            foreach (string item in im)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    
    }
//定义类
    public class IterationMonths : IEnumerable
    {
        string[] Dat = { "Jan", "Fer", "Mar", "Apr", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };

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

 

posted on 2022-05-28 11:22  sbwynnss  阅读(20)  评论(0)    收藏  举报