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