前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#-IEnumerator与IEnumerable

C#-IEnumerator与IEnumerable

作者头像
祝你万事顺利
发布2019-08-23 17:44:52
8450
发布2019-08-23 17:44:52
举报
文章被收录于专栏:Unity游戏开发Unity游戏开发

IEnumerable是所有可迭代非范型类的基础接口。IEnumerable包括一个方法GetEnumerator方法,方法返回一个IEnumerator。

IEnumerator是所有非范型迭代器的基础接口。foreach语句隐藏了C#迭代器的复杂实现。推荐使用foreach代替直接操作迭代器。 迭代器可以读取集合中的数据,但是不能从底层修改集合。 初始的时候,迭代器定位在集合的第一个元素前面,在读取Current值之前需要调用一次MoveNext将迭代器驱动到第一个元素的位置。 Current一直返回相同的元素直到调用了MoveNext或者Reset方法。MoveNext将Current推进到下一个元素。 如果MoveNext之后position超出了集合的范围,MoveNext将返回false。 通过调用Reset将Current重置到第一个元素之前。

于是我们不难得出

代码语言:javascript
复制
class Program
    {
        static void Main(string[] args)
        {
            Person[] peopleArray = new Person[3]
            {
                new Person("John","Smith"),
                new Person("Tom","Johnson"),
                new Person("Sue","Robon"),
            };
            People peopleList = new People(peopleArray);
            foreach (Person person in peopleList)
            {
                Console.WriteLine(person.firstName + person.lastName);
            }

            IEnumerator enumerator = peopleList.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Person person = (Person)enumerator.Current;
                Console.WriteLine(person.firstName + person.lastName);
            }
        }
    }

上面两种写法是一样的,foreach是一种语法糖,简化了遍历其中的具体实现。被遍历的类通过实现IEnumerable接口和实现一个IEnumerator枚举器实现遍历功能。

使用实例:

代码语言:javascript
复制
class Program
    {
        static void Main(string[] args)
        {
            Person[] peopleArray = new Person[3]
            {
                new Person("John","Smith"),
                new Person("Tom","Johnson"),
                new Person("Sue","Robon"),
            };
            People peopleList = new People(peopleArray);
            foreach (Person person in peopleList)
            {
                Console.WriteLine(person.firstName + person.lastName);
            }
        }
    }
    public class Person
    {
        public string firstName;
        public string lastName;

        public Person(string firstName, string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }

    public class People : IEnumerable
    {
        private Person[] _people;
        public People(Person[] pArray)
        {
            _people = new Person[pArray.Length];
            for (int i = 0; i < pArray.Length; i++)
            {
                _people[i] = pArray[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)GetEnumerator();
        }

        public PeopleEnum GetEnumerator()
        {
            return new PeopleEnum(_people);
        }
    }

    public class PeopleEnum:IEnumerator
    {
        public Person[] _people;
        int position = -1;
        public PeopleEnum(Person[] list)
        {
            _people = list;
        }

        Object IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }

        public Person Current
        {
            get
            {
                try
                {
                    return _people[position];
                }
                catch(IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        public bool MoveNext()
        {
            position++;
            return (position < _people.Length);
        }

        public void Reset()
        {
            position = -1;
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.08.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档