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