C# IEnumerator枚举器

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C# IEnumerator枚举器

霍比特X   2022-06-28 我要评论

1、对象只要一个类型实现了IEnumerable接口就能遍历
2、IEnumerator是枚举器,一个接口类,实现MoveNext->Current->Reset
3、yield关键字是一个迭代器,相当于实现了IEnumerator枚举器
4、IEnumerable是可枚举类型,IEnumerator是枚举器

public class IEnumerableShow {
        public void Show() {
            int[] array = { 1, 2, 3, 4, 5 };
            Student student = new Student { Id = 1 };
            foreach (var item in array) {
                Console.WriteLine();
            }
        }
    }

    class Student:IEnumerable { 
        public int Id { get; set; }
        public IEnumerator GetEnumerator() { //返回一个枚举器
            //yield return "Ant编程1";
            //yield return "Ant编程2";
            //yield return "Ant编程3";
            string[] student = { "Ant编程1", "Ant编程2", "Ant编程3" };
            return new StudentEnumerator(student);
        }
    }

    internal class StudentEnumerator : IEnumerator
    {
        string[] _student;
        int _position = -1;
        public StudentEnumerator(string[] student) {
            this._student = student;
        }

        public object Current {
            get {
                if (_position == -1) {
                    throw new InvalidOperationException();
                }
                if (_position>=_student.Length) {
                    throw new InvalidOperationException();
                }
                return _student[_position];
            }
        }

        public bool MoveNext()
        {
            if (_position<_student.Length-1) {
                _position++;
                return true;
            }
            else {
                return false;
            }
        }

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

IEnumerator , IEnumerable 枚举器接口

IEnumerator是枚举器的意思,IEnumerable是可枚举的意思。这两个都是个接口

foreach是一种语法糖,用来简化对可枚举元素的遍历代码。而被遍历的类通过实现IEnumerable接口和一个相关的IEnumerator枚举器来实现遍历功能。

public class MyList : IEnumerable
{
    public int[] _data = new int[10] { 1, 5, 7, 9, 7, 8, 7, 8, 7, 4 };
 
    public int this[int index]
    {
        get
        {
            return _data[index];
        }
    }
 
    IEnumerator IEnumerable.GetEnumerator()
    {
        Debug.Log("foreach调用  GetEnumerator");
        return new MIEnumtor(this);
    }
}
public class MIEnumtor : IEnumerator
{
    private MyList myList;
 
    private int index;
 
   public  MIEnumtor(MyList my)
    {
        index = -1;
        myList = my;
    }
 
    public object Current
    {
        get
        {
            Debug.Log("foreach调用  Current");
            return myList[index];
        }
    }
 
    public bool MoveNext()
    {
        Debug.Log("foreach调用  MoveNext");
        if (index < myList._data.Length - 1)
        {
            index++;
            return true;
        }
        index = -1;
        return false;
    }
 
    public void Reset()
    {
 
    }
}

GetIEnumerator()负责获取枚举器。
MoveNext()负责让Current获取下一个值,并判断遍历是否结束。
Current负责返回当前指向的值。
Rest()负责重置枚举器的状态(在foreach中没有用到)
这些就是IEnumerable,IEnumerator的基本工作原理了。

        MyList my = new MyList();
 
        foreach (var item in my)
        {
            Debug.Log(item);
        }

等价于

        MyList my = new MyList();
 
        MIEnumtor mIEnumtor = my.GetEnumerator();
 
        while (mIEnumtor.MoveNext())
        {
            Debug.Log(mIEnumtor.Current);
        }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们