IEnumerable IEnumerator 接口

一直以来,搞不懂IEnumerable与IEnumerator这两个接口的区别,今天看了一下MSDN并在网上搜了一把关于他们的区别,似乎理解了,但还是弄不懂他们最主要的区别是什么。看看MSDN是怎么定义他们的吧。

        //公开枚举数,该枚举数支持在非泛型集合上进行简单迭代 
        public interface IEnumerable 
        { 
            //返回可循环访问集合的枚举数 
            IEnumerator GetEnumerator(); 
        } 
        //支持对非泛型集合的简单迭代 
        public interface IEnumerator 
        { 
            //将枚举数推进到集合的下一个元素。 
            //如果枚举数成功地推进到下一个元素,则为 true; 
            //如果枚举数越过集合的结尾,则为 false 
            bool MoveNext(); 
            //将枚举数设置为其初始位置,该位置位于集合中第一个元素之前 
            void Reset(); 
            //获取集合中的当前元素 
            Object Current { get; } 
        } 
    }

再贴一个MSDN给出的例子:

 public class Person 
    { 
        public string firstName; 
        public string lastName; 
        public Person(string fName, string lName) 
        { 
            this.firstName = fName; 
            this.lastName = lName; 
        } 
    } 
    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]; 
            } 
        } 
        public IEnumerator GetEnumerator() 
        { 
            return new PeopleEnum(_people); 
        } 
    } 
    public class PeopleEnum : IEnumerator 
    { 
        public Person[] _people; 
        // Enumerators are positioned before the first element  
        // until the first MoveNext() call.  
        int position = -1; 
        public PeopleEnum(Person[] list) 
        { 
            _people = list; 
        } 
        public bool MoveNext() 
        { 
            position++; 
            return (position < _people.Length); 
        } 
        public void Reset() 
        { 
            position = -1; 
        } 
        public object Current 
        { 
            get 
            { 
                try 
                { 
                    return _people[position]; 
                } 
                catch (IndexOutOfRangeException) 
                { 
                    throw new InvalidOperationException(); 
                } 
            } 
        } 
    } 
    class App 
    { 
        static void Main() 
        { 
            Person[] peopleArray = new Person[3] 
        { 
            new Person("John", "Smith"), 
            new Person("Jim", "Johnson"), 
            new Person("Sue", "Rabon"), 
        }; 
            People peopleList = new People(peopleArray); 
            foreach (Person p in peopleList) 
                Console.WriteLine(p.firstName + " " + p.lastName); 
        } 
    } 
    /**/ 
    /* This code produces output similar to the following: 
*  
* John Smith 
* Jim Johnson 
* Sue Rabon 
*  
*/

IEnumerable和IEnumerator有什么区别?这是一个很让人困惑的问题(在很多forum里都看到有人在问这个问题)。研究了半天,得到以下几点认识:

1、一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回IEnumerator object)。

2、IEnumerator object具体实现了iterator(通过MoveNext(),Reset(),Current)。

3、从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举 (enumerable)”的,但并没有说明如何实现枚举器(iterator);IEnumerator是一个实现式的接口,IEnumerator object就是一个iterator。

4、IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,client可 以通过IEnumerable的GetEnumerator()得到IEnumerator object,在这个意义上,将GetEnumerator()看作IEnumerator object的factory method也未尝不可。

IEnumerator   是所有枚举数的基接口。  
  枚举数只允许读取集合中的数据。枚举数无法用于修改基础集合。  

  最初,枚举数被定位于集合中第一个元素的前面。Reset   也将枚举数返回到此位置。在此位置,调用Current 会引发异常。因此,在读取Current 的值之前,必须调用 MoveNext   将枚举数提前到集合的第一个元素。  

  在调用 MoveNext 或 Reset 之前,Current 返回同一对象。MoveNext 将Current 设置为下一个元素。   
  在传递到集合的末尾之后,枚举数放在集合中最后一个元素后面,且调用MoveNext 会返回   false。如果最后一次调用 MoveNext 返回false,则调用Current会引发异常。若要再次将Current设置为集合的第一个元素,可以调用Reset,然后再调用 MoveNext。  

  只要集合保持不变,枚举数就将保持有效。如果对集合进行了更改(例如添加、修改或删除元素),则该枚举数将失效且不可恢复,并且下一次对   MoveNext   或   Reset   的调用将引发   InvalidOperationException。如果在   MoveNext   和   Current   之间修改集合,那么即使枚举数已经无效,Current   也将返回它所设置成的元素。   
  枚举数没有对集合的独占访问权;因此,枚举一个集合在本质上不是一个线程安全的过程。甚至在对集合进行同步处理时,其他线程仍可以修改该集合,这会导致枚 举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。  

原文地址:https://www.cnblogs.com/chenying99/p/2153959.html