IEnumerable 和IEnumerator

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

public interface IEnumerable
 {
   IEnumerator GetEnumerator();
 }

 6public interface IEnumerator
 {
   bool MoveNext();
   void Reset();

   Object Current { get; }
}
 using System;
 using System.Collections;
 public class Person
 {
    public Person(string fName, string lName)
    {
      this.firstName = fName;
       this.lastName = lName;
   }
   public string firstName;
   public string 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];
        }
    }
   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);
    }
}

最后看一段别人的总结,水平高的人应该理解更深吧。我先贴出来供以后参考。

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 也将返回它所设置成的元素。

枚举数没有对集合的独占访问权;因此,枚举一个集合在本质上不是一个线程安全的过程。甚至在对集合进行同步处理时,其他线程仍可以修改该集合,这会导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。

参考资料 http://www.cnblogs.com/illele/archive/2008/04/21/1164696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值