欢迎访问我的博客 https://javascript.shop

重新复习基础草稿:迭代器的使用

原文发布时间为:2008-12-05 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;

namespace fanxing1
{
    class Class1
    {
        static void Main()
        {
            ListClass listClass1 = new ListClass();

            foreach (int i in listClass1)
            {
                System.Console.WriteLine(i);
            }

            foreach (int n in listClass1.SampleIterator(50,60))
            {
                System.Console.WriteLine(n);
            }

            SampleCollection col = new SampleCollection();
            // Display the collection items:
            System.Console.WriteLine("Values in the collection are:");
            foreach (int i in col.BuildCollection())
            {
                System.Console.Write(i + " ");
            }

            Console.ReadLine();
        }
    }

    public class ListClass
    {
        public IEnumerator GetEnumerator()
        {
            for (int j = 0; j < 10; j++)
            {
                yield return j;
            }
            yield return 96;
            yield return 100;
        }

        public IEnumerable SampleIterator(int start, int end)
        {
            for (int i = start; i <= end; i++)
            {
                yield return i;
            }
        }
     
    }

    public class SampleCollection
    {
        public int[] items;

        public SampleCollection()
        {
            items = new int[5] { 5, 4, 7, 9, 3 };
        }

        public IEnumerable BuildCollection()
        {
            for (int i = 0; i < items.Length; i++)
            {
                yield return items[i];
            }
        }
    }

}

posted @ 2017-07-11 00:37  孑孓子  阅读(124)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop