C#中IEnumerable、ICollection、IList、List之間的區(qū)別
IEnumerable、ICollection、IList、List之間的區(qū)別,本文分別分析了它的實現(xiàn)源碼,從而總結(jié)出了它們之間的關(guān)系和不同之處。
首先我看看 IEnumerable:
// 摘要: // 公開枚舉器,該枚舉器支持在指定類型的集合上進行簡單迭代。 // // 類型參數(shù): // T: // 要枚舉的對象的類型。 [TypeDependency("System.SZArrayHelper")] public interface IEnumerable<out T> : IEnumerable { // 摘要: // 返回一個循環(huán)訪問集合的枚舉器。 // // 返回結(jié)果: // 可用于循環(huán)訪問集合的 System.Collections.Generic.IEnumerator<T>。 IEnumerator<T> GetEnumerator(); }
IEnumerable<T> 實現(xiàn)IEnumerable接口方法,那IEnumberable做什么的,其實就提高可以循環(huán)訪問的集合。說白了就是一個迭代。
再來看看ICollection:
// 摘要: // 定義操作泛型集合的方法。 // // 類型參數(shù): // T: // 集合中元素的類型。 [TypeDependency("System.SZArrayHelper")] public interface ICollection<T> : IEnumerable<T>, IEnumerable
原來ICollection<T> 同時繼承IEnumerable<T>和IEnumerable兩個接口,按我的理解就是,ICollection繼續(xù)它們2個接口而且擴展了方法,功能強多了。
我們繼續(xù)看IList:
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
IList 繼承它們?nèi)齻€接口,怪不得功能這么多啊
最后來看看List:
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
它們都是接口,只有List 是類,不僅實現(xiàn)它們的接口,而且還擴展了太多的方法給我利用,幾乎所有功能都能實現(xiàn)了。
按照功能排序: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接口,提供了同步處理、賦值及返回內(nèi)含元素數(shù)目的功能
一、ICollection接口的原型
namespace System.Collections { // 摘要: // 定義所有非泛型集合的大小、枚舉器和同步方法。 [ComVisible(true)] public interface ICollection : IEnumerable { // 摘要: // 獲取 System.Collections.ICollection 中包含的元素數(shù)。 // // 返回結(jié)果: // System.Collections.ICollection 中包含的元素數(shù)。 int Count { get; } // // 摘要: // 獲取一個值,該值指示是否同步對 System.Collections.ICollection 的訪問(線程安全)。 // // 返回結(jié)果: // 如果對 System.Collections.ICollection 的訪問是同步的(線程安全),則為true;否則為false。 bool IsSynchronized { get; } // // 摘要: // 獲取一個可用于同步對 System.Collections.ICollection 的訪問的對象。 // // 返回結(jié)果: // 可用于同步對 System.Collections.ICollection 的訪問的對象。 object SyncRoot { get; } // 摘要: // 從特定的 System.Array 索引處開始,將 System.Collections.ICollection 的元素復(fù)制到一個 System.Array // 中。 // // 參數(shù): // array: // 作為從 System.Collections.ICollection 復(fù)制的元素的目標(biāo)位置的一維 System.Array。System.Array // 必須具有從零開始的索引。 // // index: // array 中從零開始的索引,將在此處開始復(fù)制。 // // 異常: // System.ArgumentNullException: // array 為null。 // // System.ArgumentOutOfRangeException: // index 小于零。 // // System.ArgumentException: // array 是多維的。- 或 -源 System.Collections.ICollection 中的元素數(shù)目大于從index 到目標(biāo) array // 末尾之間的可用空間。 // // System.ArgumentException: // 源 System.Collections.ICollection 的類型無法自動轉(zhuǎn)換為目標(biāo) array 的類型。 void CopyTo(Array array,int index); } }
二、IEnumerable接口
1、IEnumerable接口是ICollection的父接口,凡實現(xiàn)此接口的類,都具備“可迭代”的能力。
2、IEnumerable接口只定義了一個方法:GetEnumerator,該方法將返回一個“迭代子”對象(或稱為迭代器對象),是一個實現(xiàn)了IEnumerator接口的對象實例。
3、凡是實現(xiàn)了IEnumerable接口的類,都可以使用foreach循環(huán)迭代遍歷。
三、簡單的ICollection實現(xiàn)范例
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>是可以統(tǒng)計集合中對象的標(biāo)準(zhǔn)接口。該接口可以確定集合的大?。–ount),集合是否包含某個元素(Contains),復(fù)制集合到另外一個數(shù)組(ToArray),集合是否是只讀的(IsReadOnly)。如果一個集合是可編輯的,那么可以調(diào)用Add,Remove和Clear方法操作集合中的元素。因為該接口繼承IEnumerable<T>,所以可以使用foreach語句遍歷集合。
ICollection<T>定義源碼
public interface ICollection<T> : IEnumerable<T> { // Numberof itemsin the collections. int Count { get; } bool IsReadOnly { get; } voidAdd(T item); void Clear(); boolContains(T item); // CopyTo copies a collectioninto an Array, startingat 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); }
到此這篇關(guān)于C#中IEnumerable、ICollection、IList、List之間的區(qū)別的文章就介紹到這了,更多相關(guān)C# IEnumerable、ICollection、IList、List內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#利用Openxml讀取Excel數(shù)據(jù)實例
這篇文章主要介紹了C#利用Openxml讀取Excel數(shù)據(jù)的方法,包括使用中的注意點分析及疑難探討,需要的朋友可以參考下2014-09-09C# 表達(dá)式目錄樹Expression的實現(xiàn)
本文主要介紹了C# 表達(dá)式目錄樹Expression的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09UGUI實現(xiàn)隨意調(diào)整Text中的字體間距
這篇文章主要為大家詳細(xì)介紹了UGUI實現(xiàn)隨意調(diào)整字體間距的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03C#中結(jié)構(gòu)體定義并轉(zhuǎn)換字節(jié)數(shù)組詳解
在寫C#TCP通信程序時,發(fā)送數(shù)據(jù)時,只能發(fā)送byte數(shù)組,處理起來比較麻煩不說,如果是和VC6.0等寫的程序通信的話,很多的都是傳送結(jié)構(gòu)體,在VC6.0中可以很方便的把一個char[]數(shù)組轉(zhuǎn)換為一個結(jié)構(gòu)體,而在C#卻不能直接把byte數(shù)組轉(zhuǎn)換為結(jié)構(gòu)體,要在C#中發(fā)送結(jié)構(gòu)體,應(yīng)該怎么做呢?2017-11-11WinForm項目開發(fā)中WebBrowser用法實例匯總
這篇文章主要介紹了WinForm項目開發(fā)中WebBrowser用法,需要的朋友可以參考下2014-08-08