C#中迭代器的实现进程

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

namespace _6_1
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] values = { "a", "b", "c", "d", "e" };
            IterationSample conllection = new IterationSample(values, 3);
            foreach (string x in conllection)
            {
                Console.WriteLine(x);
            }
            Console.ReadKey();
        }
    }
    class IterationSample : IEnumerable
    {
        object[] values;
        int startingPoint;
        public IterationSample(object[] values, int startingPoint)
        {
            this.values = values;
            this.startingPoint = startingPoint;
        }
        public IEnumerator GetEnumerator()
        {
            //throw new NotImplementedException();
            return new IterationSampleIterator(this);
        }
        //class IterationSampleIterator : IEnumerator
        //{
        //    IterationSample parent;
        //    int position;

        //    internal IterationSampleIterator(IterationSample parent)
        //    {
        //        this.parent = parent;
        //        position = -1;
        //    }

        //    public bool moveNext()
        //    {
        //        if (position != parent.values.Length)
        //        {
        //            position++;
        //        }
        //        return position < parent.values.Length;
        //    }
        //    public object Current
        //    {
        //        get
        //        {
        //            if (position == -1 || position == parent.values.Length)
        //            {
        //                throw new InvalidOperationException();
        //            }
        //            int index = position + parent.startingPoint;
        //            index = index % parent.values.Length;
        //            return parent.values[index];
        //        }
        //    }

        //    public void reset()
        //    {
        //        position = -1;
        //    }
        //}
        class IterationSampleIterator : IEnumerator
        {
            IterationSample parent;
            int position;

            internal IterationSampleIterator(IterationSample parent)
            {
                this.parent = parent;
                position = -1;
            }

            public bool MoveNext()
            {
                if (position != parent.values.Length)
                {
                    position++;
                }
                return position < parent.values.Length;
            }

            public object Current
            {
                get
                {
                    if (position == -1 ||
                        position == parent.values.Length)
                    {
                        throw new InvalidOperationException();
                    }
                    int index = (position + parent.startingPoint);
                    index = index % parent.values.Length;
                    return parent.values[index];
                }
            }

            public void Reset()
            {
                position = -1;
            }
        }
    }
}

C#2中实现迭代器

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

namespace _6_4
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }
        public IEnumerator GetEnumerator() {
            int staringPoint=1;
            object [] values={"a","b","c","d","e"};
            for (int index = 0; index < values.Length;index++ ) {
                yield return values[(index + staringPoint) % values.Length];
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值