C#迭代的一般应用

2022/5/29 1:23:08

本文主要是介绍C#迭代的一般应用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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];
            }
        }
    }
}

 



这篇关于C#迭代的一般应用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程