C#-IEnumerator與IEnumerable

IEnumerable是所有可迭代非范型類的基礎(chǔ)接口。IEnumerable包括一個方法GetEnumerator方法,方法返回一個IEnumerator。

IEnumerator是所有非范型迭代器的基礎(chǔ)接口。foreach語句隱藏了C#迭代器的復(fù)雜實現(xiàn)。推薦使用foreach代替直接操作迭代器。
迭代器可以讀取集合中的數(shù)據(jù),但是不能從底層修改集合。
初始的時候,迭代器定位在集合的第一個元素前面,在讀取Current值之前需要調(diào)用一次MoveNext將迭代器驅(qū)動到第一個元素的位置。
Current一直返回相同的元素直到調(diào)用了MoveNext或者Reset方法。MoveNext將Current推進到下一個元素。
如果MoveNext之后position超出了集合的范圍,MoveNext將返回false。
通過調(diào)用Reset將Current重置到第一個元素之前。

于是我們不難得出

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是一種語法糖,簡化了遍歷其中的具體實現(xiàn)。被遍歷的類通過實現(xiàn)IEnumerable接口和實現(xiàn)一個IEnumerator枚舉器實現(xiàn)遍歷功能。

使用實例:

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;
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容