IEnumerable、ICollection、IList、List之間的區別,本文分別分析了它的實作原始碼,從而總結出了它們之間的關系和不同之處,
首先我看看 IEnumerable:
1 // 摘要: 2 // 公開列舉器,該列舉器支持在指定型別的集合上進行簡單迭代, 3 // 4 // 型別引數: 5 // T: 6 // 要列舉的物件的型別, 7 [TypeDependency("System.SZArrayHelper")] 8 public interface IEnumerable<out T> : IEnumerable 9 { 10 // 摘要: 11 // 回傳一個回圈訪問集合的列舉器, 12 // 13 // 回傳結果: 14 // 可用于回圈訪問集合的 System.Collections.Generic.IEnumerator<T>, 15 IEnumerator<T> GetEnumerator(); 16 }
IEnumerable<T> 實作IEnumerable介面方法,那IEnumberable做什么的,其實就提高可以回圈訪問的集合,說白了就是一個迭代,
再來看看ICollection:
1 // 摘要: 2 // 定義操作泛型集合的方法, 3 // 4 // 型別引數: 5 // T: 6 // 集合中元素的型別, 7 [TypeDependency("System.SZArrayHelper")] 8 public interface ICollection<T> : IEnumerable<T>, IEnumerable
原來ICollection<T> 同時繼承IEnumerable<T>和IEnumerable兩個介面,按我的理解就是,ICollection繼續它們2個介面而且擴展了方法,功能強多了,
我們繼續看IList:
1 public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
IList 繼承它們三個介面,怪不得功能這么多啊
最后來看看List:
1 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介面的原型
1 namespace System.Collections 2 { 3 // 摘要: 4 // 定義所有非泛型集合的大小、列舉器和同步方法, 5 [ComVisible(true)] 6 public interface ICollection : IEnumerable 7 { 8 // 摘要: 9 // 獲取 System.Collections.ICollection 中包含的元素數, 10 // 11 // 回傳結果: 12 // System.Collections.ICollection 中包含的元素數, 13 int Count { get; } 14 // 15 // 摘要: 16 // 獲取一個值,該值指示是否同步對 System.Collections.ICollection 的訪問(執行緒安全), 17 // 18 // 回傳結果: 19 // 如果對 System.Collections.ICollection 的訪問是同步的(執行緒安全),則為 true;否則為 false, 20 bool IsSynchronized { get; } 21 // 22 // 摘要: 23 // 獲取一個可用于同步對 System.Collections.ICollection 的訪問的物件, 24 // 25 // 回傳結果: 26 // 可用于同步對 System.Collections.ICollection 的訪問的物件, 27 object SyncRoot { get; } 28 29 // 摘要: 30 // 從特定的 System.Array 索引處開始,將 System.Collections.ICollection 的元素復制到一個 System.Array 31 // 中, 32 // 33 // 引數: 34 // array: 35 // 作為從 System.Collections.ICollection 復制的元素的目標位置的一維 System.Array,System.Array 36 // 必須具有從零開始的索引, 37 // 38 // index: 39 // array 中從零開始的索引,將在此處開始復制, 40 // 41 // 例外: 42 // System.ArgumentNullException: 43 // array 為 null, 44 // 45 // System.ArgumentOutOfRangeException: 46 // index 小于零, 47 // 48 // System.ArgumentException: 49 // array 是多維的,- 或 -源 System.Collections.ICollection 中的元素數目大于從 index 到目標 array 50 // 末尾之間的可用空間, 51 // 52 // System.ArgumentException: 53 // 源 System.Collections.ICollection 的型別無法自動轉換為目標 array 的型別, 54 void CopyTo(Array array, int index); 55 } 56 }
二、IEnumerable介面
1、IEnumerable介面是ICollection的父介面,凡實作此介面的類,都具備“可迭代”的能力,
2、IEnumerable介面只定義了一個方法:GetEnumerator,該方法將回傳一個“迭代子”物件(或稱為迭代器物件),是一個實作了IEnumerator介面的物件實體,
3、凡是實作了IEnumerable介面的類,都可以使用foreach回圈迭代遍歷,
三、簡單的ICollection實作范例
1 public class MyCollectioin:ICollection 2 { 3 private string[] list; 4 private object root; 5 6 public MyCollection() 7 { 8 list = new string[3]{"1","3","4"}; 9 } 10 11 #region ICollection Members 12 public bool IsSynchronized 13 { 14 get{ 15 return true; 16 } 17 } 18 19 public int Count 20 { 21 get 22 { 23 return list.Length; 24 } 25 } 26 27 public void CopyTo(Array array,int index) 28 { 29 list.CopyTo(array,index); 30 } 31 32 public object SyncRoot 33 { 34 get 35 { 36 return root; 37 } 38 } 39 #endregioin 40 41 #region IEnumerable Members 42 public IEnumerable GetEnumerator() 43 { 44 return list.GetEnumerator(); 45 } 46 47 #endregion 48 }
四、ICollection<T>
ICollection<T>是可以統計集合中物件的標準介面,該介面可以確定集合的大小(Count),集合是否包含某個元素(Contains),復制集合到另外一個陣列(ToArray),集合是否是只讀的(IsReadOnly),如果一個集合是可編輯的,那么可以呼叫Add,Remove和Clear方法操作集合中的元素,因為該介面繼承IEnumerable<T>,所以可以使用foreach陳述句遍歷集合,
ICollection<T>定義原始碼
1 public interface ICollection<T> : IEnumerable<T> 2 { 3 // Number of items in the collections. 4 int Count { get; } 5 6 bool IsReadOnly { get; } 7 8 void Add(T item); 9 10 void Clear(); 11 12 bool Contains(T item); 13 14 // CopyTo copies a collection into an Array, starting at a particular 15 // index into the array. 16 // 17 void CopyTo(T[] array, int arrayIndex); 18 19 //void CopyTo(int sourceIndex, T[] destinationArray, int destinationIndex, int count); 20 21 bool Remove(T item); 22 }
本文轉自:https://www.cnblogs.com/sunliyuan/p/5816666.html
學習Mark
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/55333.html
標籤:其他
上一篇:C# MVC 全域錯誤Application_Error中處理(包括Ajax請求)
下一篇:gRPC搭建使用方式
