C# Iterator迭代器的实现方式

C#发展到今天又三种方式实现迭代:

1、非泛型非 yield,这个较简单,代码如下:

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

namespace ConsoleApplication3
{
    public  class MyIterator:IEnumerable
    {
        public int[] testInt = new int[3] { 2, 56, 34 };
        public IEnumerator GetEnumerator()
        {
            return new MyEnumerator(this);
        }
        private class MyEnumerator : IEnumerator
        {
            private int currentIndex = -1;
            private int[] dataSource;
            public MyEnumerator(MyIterator mit)
            {
                this.dataSource = mit.testInt;
            }
            public bool MoveNext()
            {
                currentIndex++;
                return currentIndex < this.dataSource.Length;
            }
            public object Current { get { return this.dataSource[currentIndex]; } }
            public void Reset()
            {
                currentIndex = 0;
            }
        }
    }
}

调用:

 MyIterator mi = new MyIterator();
            foreach (int i in mi)
            {
                Console.WriteLine(i);
            }

            Console.ReadLine();

 

2、泛型方法实现,这个要写的代码比较多:

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

namespace ConsoleApplication3
{
    public class MyIteratorGener : IEnumerable<int>
    {
        public int[] testInt = new int[3] { 2, 56, 34 };
        //实现的是泛型接口 IEnumerable<int> 里面的 IEnumerator<T> GetEnumerator();
        public IEnumerator<int> GetEnumerator()
        {
            return new MyEnumerator(this);
        }
       //由于  IEnumerable<int>继承IEnumerable,所以要实现IEnumerator GetEnumerator(); 这个,不然会报错
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
        private class MyEnumerator : IEnumerator<int>, IDisposable
        {
            private int currentIndex = -1;
            private int[] dataSource;
            public MyEnumerator(MyIteratorGener mit)
            {
                this.dataSource = mit.testInt;
            }

            public bool MoveNext()
            {
                currentIndex++;
                return currentIndex < this.dataSource.Length;
            }
           //实现的是IEnumerator< T> 中的 T Current { [__DynamicallyInvokable] get; }
            public int Current { get { return this.dataSource[currentIndex]; } }
           // 由于IEnumerable<T>继承IEnumerable,两个接口的GetEnumerator方法同名  
    // 所以对于非泛型的IEnumerable的GetEnumerator方法使用显式接口实现。  
    // (如果IEnumerable<T>没有继承IEnumerable,那么一个只支持使用非泛型IEnumerable的  
    // 方法将无法使用IEnumerable<T>)  
//不然会报错,未实现 object Ienumerator.Current
object IEnumerator.Current { get { return this.dataSource[currentIndex]; } } public void Reset() { currentIndex = 0; } // IEnumerator<T>继承了IDisposable,为了遍历完后清理状态,所以需要实现该方法 // 该方法在foreach循环完毕后自动调用 public void Dispose() { } } } }

调用:

MyIteratorGener mi = new MyIteratorGener();
            foreach (int i in mi)
            {
                Console.WriteLine(i);
            }

            Console.ReadLine();

 

3、Yield return实现。C#2.0才可以   使用 yield break 结束一个迭代.

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

namespace ConsoleApplication3
{
    public class MyIteratorYield:IEnumerable
    {
        public IEnumerator GetEnumerator()
        {
           yield return 2;
           yield return 56;
           yield return 34;
        }
    }
}

 调用:

MyIteratorYield mi = new MyIteratorYield();
            foreach (int i in mi)
            {
                Console.WriteLine(i);
            }

            Console.ReadLine();

 

转载于:https://www.cnblogs.com/huaan011/p/5596271.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值