代码改变世界

IList,ICollection,IEnumerable辨析 (转)

2013-03-05 22:57  Sam Jin  阅读(843)  评论(0)    收藏  举报

祖宗: IEnumerable

此接口只有一个方法 GetEnumerator();

是FrameWork为了实现迭代器模式设计的接口。所有继承了IEnumerable的类,要使用foreach迭代器时,就需要使用该方法。因此也只有实现了该接口的类才可以使用foreach。

ICollection继承自IEnumerable,IList继承自ICollection

这两个接口都是为了给集合提供一些公用的方法。只是分了两个层次,IList比ICollection多几个方法,增加,移除成员。可以简单理解为:ICollection主要针对静态集合;IList主要针对动态集合

IListIList,ICollection,IEnumerable在命名空间System.Collections中。

IList<T>,ICollection<T>,IEnumerable<T>。在System.Collections.Generic 命名空间中。

IList<T>,ICollection<T>,IEnumerable<T>。是2.0引入泛型以后新增的。主要是提高重用性与类型安全。

IEnumerable<T>继承自IEnumerable

ICollection<T>继承自IEnumerable<T>

IList<T>继承自ICollection<T>

因此可以完全使用泛型接口,而放弃使用ICollection和IList。泛型接口提供了更好的类型安全和编译时的检验。

补充:

IEnumerable<T>和IEnumerable都只有一个方法。

ICollection<T>和ICollection的结构是不一样的。ICollection<T>比ICollection多几个方法。它包含了几个IList中的几个方法。也许是对以前的改进。

 

 

IEnumerable<T>继承自IEnumerable
ICollection<T>继承自IEnumerable<T>
IList<T>继承自ICollection<T>

 

Enumerable接口:

    实现了IEnumerable接口的集合表明该集合能够提供一个enumerator(枚举器)对象,支持当前的遍历集合。IEnumerable接口只有一个成员GetEnumerator()方法。


IEnumerator接口是只读的,包括以下三个成员:

    MoveNext()方法调整遍历指针移向集合的下一个元素。注意,遍历指针的初始位置是集合中第一个元素的前面。要指向第一个元素,必须先调用一次MoveNext()方法。该方法返回一个布尔值,如果成功遍历到下一个元素,则返回true;如果指针移出末尾,则返回false。 
    Reset()方法用于设置遍历指针指向初始位置,即集合中第一个元素的前面。 
    Current属性返回集合中当前对象的引用。 


IEnumerable和IEnumerator的区别:

    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也未尝不可。
 
ICollection集合:

    该接口是IEnumerable接口的子接口,定义了集合的大小、IEnumerator接口和同步方法。在IEnumerable接口的基础上增加了以下功能。 
    Count;该属性返回集合中元素的数目。 
    CopyTo(Array   array,   int   index);该方法用于实现从集合中拷贝元素到一个一维数组中。 


IList接口:

    Add()和Insert()方法用于向集合中添加条目,使用索引来指定项目要插入的位置,其中首元素的索引为0;Add()将新条目添加到尾部。 
    Remove()和RemoveAt()用于从列表中删除条目。Clear()用于删除所有条目。 
    IndexOf和Contains()用于搜索该列表。 
    Item属性用于获取或设置索引指定的值。C#中可以使用[]运算符进行访问。
 
IList接口和ArrayList类的目的是实现动态数组,ArrayList是IList的一个实现。
 
IDictionary接口:

    IDictionary接口是一个包含一组”关键字/值”对的数据结构,每个值都由相应的关键字来定义。关键字和值可以是任何数据类型,关键字必须唯一且非空。 
    Add()方法添加一个指定的关键字和值的条目到IDictionary接口。 
    Item属性检索指定关键字所对应的值。 
    Keys和Values属性分别返回包含所有关键字和值的集合。 
    Remove()用于删除指定关键字对应的条目。Clear()用于删除所有条目。 
    GetEnumator()返回一个IDictionaryEnumerator,可用于遍历IDictionary接口。

 

代码 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
class SimpleList : IList
{
   private object[] _contents = new object[8];
   private int _count;

   public SimpleList()
   {
      _count = 0;
   }

   // IList Members
   public int Add(object value)
   {
      if (_count < _contents.Length)
      {
         _contents[_count] = value;
         _count++;

         return (_count - 1);
      }
      else
      {
         return -1;
      }
   }

   public void Clear()
   {
      _count = 0;
   }

   public bool Contains(object value)
   {
      bool inList = false;
      for (int i = 0; i < Count; i++)
      {
         if (_contents[i] == value)
         {
            inList = true;
            break;
         }
      }
      return inList;
   }

   public int IndexOf(object value)
   {
      int itemIndex = -1;
      for (int i = 0; i < Count; i++)
      {
         if (_contents[i] == value)
         {
            itemIndex = i;
            break;
         }
      }
      return itemIndex;
   }

   public void Insert(int index, object value)
   {
      if ((_count + 1 <= _contents.Length) && (index < Count) && (index >= 0))
      {
         _count++;

         for (int i = Count - 1; i > index; i--)
         {
            _contents[i] = _contents[i - 1];
         }
         _contents[index] = value;
      }
   }

   public bool IsFixedSize
   {
      get
      {
         return true;
      }
   }

   public bool IsReadOnly
   {
      get
      {
         return false;
      }
   }

   public void Remove(object value)
   {
      RemoveAt(IndexOf(value));
   }

   public void RemoveAt(int index)
   {
      if ((index >= 0) && (index < Count))
      {
         for (int i = index; i < Count - 1; i++)
         {
            _contents[i] = _contents[i + 1];
         }
         _count--;
      }
   }

   public object this[int index]
   {
      get
      {
         return _contents[index];
      }
      set
      {
         _contents[index] = value;
      }
   }

   // ICollection Members

   public void CopyTo(Array array, int index)
   {
      int j = index;
      for (int i = 0; i < Count; i++)
      {
         array.SetValue(_contents[i], j);
         j++;
      }
   }

   public int Count
   {
      get
      {
         return _count;
      }
   }

   public bool IsSynchronized
   {
      get
      {
         return false;
      }
   }

   // Return the current instance since the underlying store is not
   // publicly available.
   public object SyncRoot
   {
      get
      {
         return this;
      }
   }

   // IEnumerable Members

   public IEnumerator GetEnumerator()
   {
      // Refer to the IEnumerator documentation for an example of
      // implementing an enumerator.
      throw new Exception("The method or operation is not implemented.");
   }

   public void PrintContents(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format("List has a capacity of {0} and currently has {1} elements.", _contents.Length, _count) + "\n";
      outputBlock.Text += "List contents:";
      for (int i = 0; i < Count; i++)
      {
         outputBlock.Text += String.Format(" {0}", _contents[i]);
      }
      outputBlock.Text += "\n";
   }
}