.NET Collection Overview
IEnumerator – A Forward Const Iterator
IEnumerator defines an enumerator, which is an analogy of forward const iterator. "Forward" means enumerator can move in only one direction - forward. "Const" means that it cannot change the underlying collection. Current property is read-only.
Objects that implement IEnumerator are usually returned from IEnumerable.GetEnumerator(). By convention, new enumerator points to a location "just before" the beginning of the collection. You should call MoveNext() to move the enumerator to the first element of the collection. MoveNext() will return false if collection is empty.
IEnumerable – An Enumerable Collection with Read-Only Forward-Only Access
IEnumerable is implemented by virtually all .NET collections. It defines a single method - GetEnumerator(). This method returns an enumerator for the collection. Any object that implements IEnumerable can be used with the foreach operator.
IQueryable – evaluate queries against a specific data source
IQueryable allows you to define parts of a query against a remote LINQ provider in multiple steps, and with deferred execution.
LINQ queries against IEnumerable<T> produce delegates (methods) which, when invoked, perform the described query. LINQ queries against IQueryable<T> produce expression trees, a data structure which represents the code that produced the query. LINQ providers such as LINQ to SQL interpret these data structures, generating the same query on the target platform (T-SQL in this case).
IObservable defines a provider for push-based notification, IObserver provides a mechanism for receiving push-based notifications. IObserver and IObservable is actually the mathematical dual of IEnumerable and IEnumerator.
ICollection – Synchronizable Collection with Count
ICollection defines a collection with Count. It defines IsSynchronized and SyncRoot properties that deal with multi-threaded access to the collection and if collection gets modified by other threads, its enumerators are invalidated and throw InvalidOperationException if used. If IsSynchronized is true, synchronization is built-in into collection operations, and collection can be used safely from multiply threads. If IsSynchronized is false, it means that collection operations are not thread-safe, and user must lock the collection instance, or use some other synchronization mechanism if he wants to work with the collection from several threads. SyncRoot is apparently supposed to return an un-synchronized version of given collection. Note that IEnumerable and IEnumerator and ICollection<T> totally ignore this issue. IReadOnlyCollection represents a strongly-typed, read-only collection of elements.
IList – Modifiable Collection with Integer Index
Represent a collection of objects than can be individually accessed by index and because of the integer index, IList behaves more like an array rather than a list.
ISet Provides the base interface for the abstraction of sets.
IDictionary – Associate Container
IDictionary defines an associative container that stores key and value pairs. The interface is relatively straightforward. IDictionaryEnumerator is a helper interface that derives from IEnumerator.
IComparer, IComparable, IEquatable<T>, IEqualityComparer
IComparer defines a method, int Compare(object x, object y), that a type implements to compare two objects whereas IComparable defines a generalized type-specific comparison method, int CompareTo(object obj), that a value type or class implements to order or sort its instance.
IEquatable defines a generalized method, Equals(T other), that a value or class implements to create a type-specific method for determining equality of instances. IEqualityComparer defines methods, bool Equals(object x, object y), int GetHashCode(object obj) to support the comparison of objects for equality.
IStructuralComparable supports structural comparison of collection objects and IStructuralEquatable defines methods to support the comparison of objects for structural equality.
Collection Generic Interfaces
System.Array – Fixed Sized Array
This class is an abstract base class for all built-in C# arrays. It represents an array of fixed size and implements IList interface. This class is interesting, because it is probably the only class in .NET that is both abstract and sealed. Thus, you can neither instantiate it, nor derive from it. Built-in C# arrays derive from it implicitly. You as a mortal user cannot declare your class abstract sealed - this causes a compilation error.
Array class definition
ArrayList – Dynamic Array
This class is a dynamic array that implements the IList interface. ArrayList can work as an adapter for arbitrary IList. Such adapters are created via static method ArrayList.Adapter(). This allows to use methods normally not available for IList, such as RemoveRange(), LastIndexOf() or Sort().
ArrayList class definition
BitArray – Array of Bits
This class implements a fixed-sized array of Boolean values, or bits. Note that it implements merely an ICollection - not a full IList. Once constructed, the size of the bit array never changes. Besides, it does not implement IList methods such as IndexOf(), Contains(), etc. From the other hand it implements a number of additional, Boolean-specific methods such as And(), Or(), and Xor()
BitArray class definition
Hash table is an un-ordered collection of key-value pairs. Key objects must correctly implement Equals() and GetHashCode.
Hashtable class definition
HashSet represents a set of values. It is based on the model of mathematical sets and provides high-performance set operations similar to accessing the keys of the Dictionary or Hashtable collections. In simple terms, the HashSet class can be thought of as a Dictionary collection without values. It provides many mathematical set operations, such as set addition (unions) and set subtraction. However, a HashSet collection is not sorted and cannot contain duplicate elements. If order or element duplication is more important than performance for your application, consider using the List class together with the Sort method.
HashSet class definition
SortedList, SortedDictionary, SortedSet
Sorted list is a sorted collection of key-value pairs. Besides implementing IDictionary interface, it also allows access to its items by integer index via GetByIndex() and SetByIndex() methods.
SortedList, SortedDictionary, SortedSet classes definition
Queue implements standard collection operations plus Enqueue(), Dequeue() and Peek() methods. Using reflection we can see that internally it uses standard array (object) - much like an ArrayList.
Queue class definition
Stack implements standard collection operations plus Push(), Pop() and Peek() methods.
Stack class definition
CollectionBase, ReadOnlyCollectionBase, DictionaryBase
These abstract classes implement IList. They represent a base for type-safe collections that manipulates concrete types instead of generic object. Internally CollecationBase is a wrapper around ArrayList.
CollectionBase, ReadOnlyCollectionBase classes definition
DictionaryBase is an abstract base for type safe dictionaries. The idea is similar to CollectionBase. Internally DictionaryBase relies on a Hashtable.
DictionaryBase classes definition
StringCollection implements type-safe collection of strings.
StringDictionary Implements string-to-object map. Interestingly enough, this class neither derives from DictionaryBase, nor implements IDictionary.
StringCollection, StringDictionary classes definition
Implements string-to-string map sorted by key.
NameValueCollection class definition
LinkedList represents a doubly linked list and because it is doubly linked, each node points forward to the Next node and backward to the Previous node. It provides separate nodes of type LinkedListNode, so insertion and removal are O(1) operations.
ListDictionary implements a dictionary based on a linked list.
ListDictionary class definition
HybridDictionary implements a dictionary based on a linked list for small sizes (below 10), and on hash table for bigger sizes.
HybridDictionary class definition