C#中IEnumerable、ICollection、IList、List之間的區別


IEnumerable、ICollection、IList、List之間的區別,本文分別分析了它的實現源碼,從而總結出了它們之間的關系和不同之處。

首先我看看 IEnumerable:

    //  摘要:
    //  公開枚舉器,該枚舉器支持在指定類型的集合上進行簡單迭代。
    //
    //  類型參數:
    //  T:
    //  要枚舉的對象的類型。
    [TypeDependency("System.SZArrayHelper")]
    public interface IEnumerable<out T> : IEnumerable
    {
        // 摘要:
        //   返回一個循環訪問集合的枚舉器。
        //
        // 返回結果:
        //   可用於循環訪問集合的 System.Collections.Generic.IEnumerator<T>。
        IEnumerator<T> GetEnumerator();
    }

 IEnumerable<T> 實現IEnumerable接口方法,那IEnumberable做什么的,其實就提高可以循環訪問的集合。說白了就是一個迭代。

再來看看ICollection:

 // 摘要:
 // 定義操作泛型集合的方法。
 //
 // 類型參數:
 // T:
 // 集合中元素的類型。
 [TypeDependency("System.SZArrayHelper")]
 public interface ICollection<T> : IEnumerable<T>, IEnumerable

原來ICollection<T> 同時繼承IEnumerable<T>和IEnumerable兩個接口,按我的理解就是,ICollection繼續它們2個接口而且擴展了方法,功能強多了。

我們繼續看IList:

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

 IList 繼承它們三個接口,怪不得功能這么多啊

最后來看看List:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

它們都是接口,只有List 是類,不僅實現它們的接口,而且還擴展了太多的方法給我利用,幾乎所有功能都能實現了。

按照功能排序:List<T> 《IList<T> 《ICollection<T>《IEnumerable<T>

按照性能排序:IEnumerable<T>《ICollection<T>《IList<T>《List<T>

 

另一種解釋:

     ICollection 接口是 System.Collections 命名空間中類的基接口,ICollection 接口擴展 IEnumerable,IDictionary 和 IList 則是擴展 ICollection 的更為專用的接口。如果 IDictionary 接口和 IList 接口都不能滿足所需集合的要求,則從 ICollection 接口派生新集合類以提高靈活性。

 

ICollection是IEnumerable的加強型接口,它繼承自IEnumerable接口,提供了同步處理、賦值及返回內含元素數目的功能

一、ICollection接口的原型

 namespace System.Collections
    {
        // 摘要:
        //     定義所有非泛型集合的大小、枚舉器和同步方法。
        [ComVisible(true)]
        public interface ICollection : IEnumerable
        {
            // 摘要:
            //     獲取 System.Collections.ICollection 中包含的元素數。
            //
            // 返回結果:
            //     System.Collections.ICollection 中包含的元素數。
            int Count { get; }
            //
            // 摘要:
            //     獲取一個值,該值指示是否同步對 System.Collections.ICollection 的訪問(線程安全)。
            //
            // 返回結果:
            //     如果對 System.Collections.ICollection 的訪問是同步的(線程安全),則為 true;否則為 false。
            bool IsSynchronized { get; }
            //
            // 摘要:
            //     獲取一個可用於同步對 System.Collections.ICollection 的訪問的對象。
            //
            // 返回結果:
            //     可用於同步對 System.Collections.ICollection 的訪問的對象。
            object SyncRoot { get; }

            // 摘要:
            //     從特定的 System.Array 索引處開始,將 System.Collections.ICollection 的元素復制到一個 System.Array
            //     中。
            //
            // 參數:
            //   array:
            //     作為從 System.Collections.ICollection 復制的元素的目標位置的一維 System.Array。System.Array
            //     必須具有從零開始的索引。
            //
            //   index:
            //     array 中從零開始的索引,將在此處開始復制。
            //
            // 異常:
            //   System.ArgumentNullException:
            //     array 為 null。
            //
            //   System.ArgumentOutOfRangeException:
            //     index 小於零。
            //
            //   System.ArgumentException:
            //     array 是多維的。- 或 -源 System.Collections.ICollection 中的元素數目大於從 index 到目標 array
            //     末尾之間的可用空間。
            //
            //   System.ArgumentException:
            //     源 System.Collections.ICollection 的類型無法自動轉換為目標 array 的類型。
            void CopyTo(Array array, int index);
        }
    }

二、IEnumerable接口

 

1、IEnumerable接口是ICollection的父接口,凡實現此接口的類,都具備“可迭代”的能力。

2、IEnumerable接口只定義了一個方法:GetEnumerator,該方法將返回一個“迭代子”對象(或稱為迭代器對象),是一個實現了IEnumerator接口的對象實例。

3、凡是實現了IEnumerable接口的類,都可以使用foreach循環迭代遍歷。

 

三、簡單的ICollection實現范例

public class MyCollectioin:ICollection
{
   private string[] list;
   private object root;
    
   public MyCollection()
   {
       list = new string[3]{"1","3","4"};
   }
 
    #region ICollection Members
    public bool IsSynchronized
    {
        get{
           return true;
        }
    }
 
     public int Count
     {
         get
         {
            return list.Length;
         }
     }
 
     public void CopyTo(Array array,int index)
     {
         list.CopyTo(array,index);
     }
     
    public object SyncRoot
    {
        get
         {
            return root;
         }
    }
    #endregioin
     
    #region IEnumerable Members
     public IEnumerable GetEnumerator()
     {
        return list.GetEnumerator();
     }
 
    #endregion
}

四、ICollection<T>

     ICollection<T>是可以統計集合中對象的標准接口。該接口可以確定集合的大小(Count),集合是否包含某個元素(Contains),復制集合到另外一個數組(ToArray),集合是否是只讀的(IsReadOnly)。如果一個集合是可編輯的,那么可以調用Add,Remove和Clear方法操作集合中的元素。因為該接口繼承IEnumerable<T>,所以可以使用foreach語句遍歷集合。

ICollection<T>定義源碼

public interface ICollection<T> : IEnumerable<T>
{
    // Number of items in the collections.       
    int Count { get; }
 
    bool IsReadOnly { get; }
 
    void Add(T item);
 
    void Clear();
 
    bool Contains(T item);
             
    // CopyTo copies a collection into an Array, starting at a particular
    // index into the array.
    //
    void CopyTo(T[] array, int arrayIndex);
             
    //void CopyTo(int sourceIndex, T[] destinationArray, int destinationIndex, int count);
 
    bool Remove(T item);
}

  

  

  

  

  

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM