C#でのIEnumerable、ICollection、IList、およびListの違い



Difference Between Ienumerable



IEnumerable、ICollection、IList、およびListの違いについて、この記事では、その実装ソースコードを個別に分析し、それらの関係と違いを要約します。

まず、IEnumerableを見てみましょう。
// Summary: // Expose the enumerator, which supports simple iteration over a collection of the specified type. // // type parameter: // T: // The type of object to enumerate. [TypeDependency('System.SZArrayHelper')] public interface IEnumerable : IEnumerable { // Summary: // Returns an enumerator that iterates through the collection. // // return result: // System.Collections.Generic.IEnumerator can be used to iterate through the collection. IEnumerator GetEnumerator() }

IEnumerableはIEnumerableインターフェイスメソッドを実装し、IEnumberableが実際に行うことにより、繰り返しアクセスできるコレクションが改善されます。率直に言って、繰り返しです。



ICollectionを見てみましょう。
// Summary: // Define methods for manipulating generic collections. // // type parameter: // T: // The type of the element in the collection. [TypeDependency('System.SZArrayHelper')] public interface ICollection : IEnumerable, IEnumerable

元々、ICollectionはIEnumerableインターフェイスとIEnumerableインターフェイスの両方を継承します。私の理解によると、ICollectionは両方を継続し、メソッドを拡張します。機能ははるかに強力です。

引き続きリストを見てみましょう。

public interface IList : ICollection, IEnumerable, IEnumerable

それらはすべてインターフェースであり、Listだけがクラスであり、それらのインターフェースを実装するだけでなく、私が使用するにはあまりにも多くのメソッドを拡張し、ほとんどすべての機能を実現できます。



(小さいものから大きいものへ)
関数で並べ替え= ':List、IList、ICollection、IEnumerable

パフォーマンスで並べ替え= ':IEnumerable、ICollection、IList、List

別の説明:



ICollectionインターフェイスはSystem.Collections名前空間のクラスの基本インターフェイスであり、ICollectionインターフェイスはIEnumerableを拡張し、IDictionaryとIListはICollectionを拡張するためのより特殊なインターフェイスです。 IDictionaryインターフェイスもIListインターフェイスも必要なコレクションの要件を満たしていない場合は、柔軟性を高めるためにICollectionインターフェイスから新しいコレクションクラスが派生します。

ICollectionは、IEnumerableの拡張インターフェイスであり、IEnumerableインターフェイスから継承し、含まれている要素の数を同期、割り当て、および返す機能を提供します。

まず、ICollectionインターフェイスのプロトタイプ
namespace System.Collections { // Summary: // Define the size, enumerator, and synchronization methods for all non-generic collections. [ComVisible(true)] public interface ICollection : IEnumerable { // Summary: // Get the number of elements contained in the System.Collections.ICollection. // // return result: // The number of elements contained in the System.Collections.ICollection. int Count { get } // // Summary: // Get a value indicating whether access to System.Collections.ICollection is synchronized (thread safe). // // return result: // true if access to System.Collections.ICollection is synchronous (thread safe) otherwise, false. bool IsSynchronized { get } // // Summary: // Get an object that can be used to synchronize access to System.Collections.ICollection. // // return result: // An object that can be used to synchronize access to System.Collections.ICollection. object SyncRoot { get } // Summary: // Copy the elements of System.Collections.ICollection to a System.Array starting at the specific System.Array index // Medium. // // Parameters: // array: // A one-dimensional System.Array that is the target location of the element copied from System.Collections.ICollection. System.Array // Must have a zero-based index. // // index: // The zero-based index in array will start copying here. // // Exception: // System.ArgumentNullException: // array is null. // // System.ArgumentOutOfRangeException: // index is less than zero. // // System.ArgumentException: // array is multidimensional. -or- The number of elements in the source System.Collections.ICollection is greater than from index to target array // Free space between the ends. // // System.ArgumentException: // The type of the source System.Collections.ICollection cannot be automatically converted to the type of the target 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

ICollectionは、コレクション内のオブジェクトをカウントできる標準のインターフェイスです。インターフェイスは、コレクションのサイズ(Count)、コレクションに要素が含まれるか(Contains)、コレクションを別の配列にコピーするか(ToArray)、コレクションが読み取り専用かどうか(IsReadOnly)を決定します。コレクションが編集可能な場合は、Add、Remove、およびClearメソッドを呼び出して、コレクション内の要素を操作できます。インターフェイスはIEnumerableを継承するため、foreachステートメントを使用してコレクションをトラバースできます。

ICollectionはソースコードを定義します
public interface ICollection : IEnumerable { // 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) }