ジェネリック | 非ジェネリック |
---|---|
IEnumerable<T> | IEnumerable |
ICollection<T> | ICollection |
IList<T> | IList |
IDictionary<TKey,TValue> | IDictionary |
IEnumerator<T> | IEnumerator |
IComparer<T> | IComparer |
IComparable<T> | IComparable |
ISet<T> |
インターフェイス | 概念 |
---|---|
IReadOnlyCollection<T> | ReadOnlyCollection<T> |
IReadOnlyList<T> | |
IReadOnlyDictionary<TKey,TValue> |
public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); }IEnumerable<T> インターフェイス (System.Collections.Generic) | Microsoft Learn
このインターフェイスを実装するオブジェクトに対しては、Enumerableクラスを介して標準クエリ演算子を適用できます。
コレクションを反復 (iterate) するための、列挙子 (enumerator) を得られます。これはforeachステートメントで利用されます。
public System.Collections.Generic.IEnumerator<out T> GetEnumerator ();IEnumerable<T>.GetEnumerator メソッド (System.Collections.Generic) | Microsoft Learn
戻り値のIEnumerator<T>はIDisposableを継承するため、破棄するようにします。
ジェネリック コレクションを操作するためのメソッドが定義されます。
public interface ICollection<T> : IEnumerable<T>, IEnumerable { int Count { get; } bool IsReadOnly { get; } void Add(T item); void Clear(); bool Contains(T item); void CopyTo(T[] array, int arrayIndex); bool Remove(T item); }ICollection<T> インターフェイス (System.Collections.Generic) | Microsoft Learn
インデックスにより個々にアクセスできるコレクションを表します。
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable { T this[int index] { get; set; } int IndexOf(T item); void Insert(int index, T item); void RemoveAt(int index); }IList<T> インターフェイス (System.Collections.Generic) | Microsoft Learn
一意の要素である、ひとそろい (set) を抽象化します。
public interface ISet<T> : ICollection<T>, IEnumerable<T>, IEnumerable { bool Add(T item); void ExceptWith(IEnumerable<T> other); void IntersectWith(IEnumerable<T> other); bool IsProperSubsetOf(IEnumerable<T> other); bool IsProperSupersetOf(IEnumerable<T> other); bool IsSubsetOf(IEnumerable<T> other); bool IsSupersetOf(IEnumerable<T> other); bool Overlaps(IEnumerable<T> other); bool SetEquals(IEnumerable<T> other); void SymmetricExceptWith(IEnumerable<T> other); void UnionWith(IEnumerable<T> other); }ISet<T> インターフェイス (System.Collections.Generic) | Microsoft Learn
public interface IEnumerator<out T> : IDisposable, IEnumerator { T Current { get; } }IEnumerator<T> インターフェイス (System.Collections.Generic) | Microsoft Learn
public interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable { int Count { get; } }IReadOnlyCollection<T> インターフェイス (System.Collections.Generic) | Microsoft Learn
public interface IReadOnlyList<out T> : IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable { T this[int index] { get; } }IReadOnlyList<T> インターフェイス (System.Collections.Generic) | Microsoft Learn
このインターフェイスを実装するCollection<T>クラスを介することで、IList<T>からIReadOnlyList<T>を得られます。
IReadOnlyList<int> items = new Collection<int>(list);
区分 | 概念 | キー | 重複 | ジェネリック | 非ジェネリック | 実装 (implement) |
---|---|---|---|---|---|---|
値のコレクション | × | ○ | List<T> | ArrayList | 配列 | |
ダブルリンク リスト | × | ○ | LinkedList<T> | なし | ||
値のコレクション。重複しない | × | × | HashSet<T> | |||
キーと値のコレクション | ○ | × | Dictionary<TKey,TValue> | Hashtable | ハッシュテーブル 注釈 | |
値で並べ替えられた、値のコレクション。重複しない | × | × | SortedSet<T> | |||
キーで並べ替えられた、キーと値のコレクション | ○ | × | SortedList<TKey,TValue> ※1 | SortedList | (並べ替えられた) 配列 注釈 | |
キーで並べ替えられた、キーと値のコレクション | ○ | × | SortedDictionary<TKey,TValue> ※1 | なし | 二分探索木 (binary search tree) 注釈 | |
先入れ先出し (FIFO) のコレクション | × | ○ | Queue<T> | Queue | 循環配列 注釈 | |
後入れ先出し (LIFO) のコレクション | × | ○ | Stack<T> | Stack |
※1 SortedList<TKey,TValue>とSortedDictionary<TKey,TValue>は下表のような違いがあります。
比較対象 | SortedList | SortedDictionary |
---|---|---|
メモリの使用量 | 小さい | 大きい |
並べ替えられたデータから一括で設定 | 早い | 遅い |
並べ替えられていないデータの挿入と除去 | 遅い | 早い |
インデックスによるキーと値へのアクセス | 可能 | 不可能 |
クラス | |
---|---|
SynchronizedCollection<T> | |
SynchronizedKeyedCollection<K,T> | |
SynchronizedReadOnlyCollection<T> |
可能ならば、Concurrent名前空間で定義されているクラスを用います。c# - What is the difference between SynchronizedCollection<T> and the other concurrent collections? - Stack Overflow
内部ではlockで同期されています。SynchronizedCollection.cs
このクラスが含まれるアセンブリSystem.ServiceModelは既定で参照されないため、明示的に参照に追加する必要があります。SynchronizedCollection<T> クラス (System.Collections.Generic) | Microsoft Learn