.NET collections refresher

I’ve been writing C#/.NET code for a long time, but every now and then I feel the need to give myself a refresher on the basics and it usually results in finding features that have been added which I knew nothing about. You know how you get used to using classes so look no further. Well here’s my refresher on the .NET collection classes as of .NET 4.5

ArrayList

The ArrayList is a wrapper around a standard object[]. It’s weakly typed and may suffer in performance tests against a generic array type such as a List<T> due to boxing/unboxing.

Unlike a standard object[] declared arrays, multidimensional arrays elements are not directly supported. Although ofcourse you can create an ArrayList or ArrayLists.

BitArray

The BitArray class is a specialized collection for handling an array of bits. It allows us to manipulate the bits in various ways, but lacks bit shifting (for example).

BlockingCollection<T>

The BlockingCollection class wraps an IProducerConsumerCollection<T> to allow thread-safe adding and removing of items. It also offers bounding capabilities.

ConcurrentBag<T>

The ConcurrentBag is a thread-safe bag. Bags are used to store objects with no concern for ordering. They support duplicates and nulls.

ConcurrentDictionary<TKey, TValue>

The ConcurrentDictionary is a thread-safe dictionary.

ConcurrentQueue<T>

The ConcurrentQueue is a thread-safe Queue (FIFO collection).

ConcurrentStack<T>

The ConcurrentStack is a thread-safe Queue (LIFO collection).

Collection<T>

The Collection class is a base class for generic collections. Whilst you can instantiate a Collection<T> directly (i.e. it’s not abstract) it’s designed more from the point of view of extensibility, i.e. deriving you own collection type from it. It only really differs from a List<T>, in that it offersvirtual methods for you to overload to customize your collection, whereas a List<T> doesn’t offer such extensibility.

Dictionary<TKey, TValue>

A key/value generic class. Duplicates are not allowed as keys, nor are nulls. For a thread-safe implementation, look at ConcurrentDictionary<TKey, TValue>.

HashSet<T>

The HashSet<T> is a set collection and thus cannot contain duplicate elements. Elements are not held in any particular order. It is aimed a set type operations, such as IntersectWith, Overlap etc.

Hashtable

The Hashtable stores key/value pairs which are organised based upon the hash code of the key. As per the Dictionary class, keys cannot be null and duplicates are not allowed. Basically it’s a weakly typed Dictionary.

HybridDictionary

The HybridDictionary is a weakly typed Dictionary which can switch between using the ListDictionary for when there’s only a few items stored and then switching to the Hashtable when the collection gets large.

ImmutableDictionary<T>

The ImmutableDictionary is, simply put, an immutable Dictionary.

See also ImmutableInterlocked .

ImmutableHashSet<T>

The ImmutableHashSet is, simply put, an immutable HashSet.

See also ImmutableInterlocked .

ImmutableList<T>

The ImmutableList is, simply put, an immutable List.

See also ImmutableInterlocked .

ImmutableQueue<T>

The ImmutableQueue is, simply put, an immutable Queue.

See also ImmutableInterlocked .

ImmutableSortedDictionary<T>

The ImmutableSortedDictionary is, simply put, an immutable SortedDictionary.

See also ImmutableInterlocked .

ImmutableSortedSet<T>

The ImmutableSortedSet is, simply put, an immutable SortedSet.

See also ImmutableInterlocked .

ImmutableStack<T>

The ImmutableStack is, simply put, an immutable Stack.

See also ImmutableInterlocked .

KeyedCollection<TKey, TItem>

The KeyedCollection is an abstract class which is a hybrid between a collection based upon an IList<T> and an IDictionary<TKey, TItem>. Unlike a Dictionary, the element stored is not a key/value pair. It’s key is embedded in it’s value.

LinkedList<T>

The LinkedList class is really that, a doubly linked list. Obviously this means insertion and removal of items are O(1) operations. Copying of elements requires far less memory than copying of array like structures, and obviously when adding more an more items the underlying data structure itself does not need to resize as an array would need to. On the downside each element requires a next and previous reference along with the data itself plus you do not get O(1) access to elements and this in itself may be a major downside – depending upon your app.

List<T>

The List is in essence this is a generic ArrayList but type safe and due to the use of generics is more efficient in performance terms when handling value types which require boxing/unboxing.

ListDictionary

This ListDictionary is a simple IDictionary implementation. It uses a singly linked list and it’s smaller and faster than a Hashtable if the number of elements is 10 or less.

NameValueCollection

Then NameValueCollection is used to store name/value pairs, however unlike say a dictionary, nulls can be used for both name and value.

ObservableCollection<T>

The ObservableCollection provide notifications when items are added to or removed from the collection or when the collection is refreshed. Used a lot with binding due to the support for INotifyPropertyChanged and INotifyCollectionChanged.

OrderedDictionary

The OrderedDictionary represents key/value data where the data may be accessed via the key or the index.

Queue<T>

A Queue is a FIFO collection. Whereby items are added to the back of the queue (using Enqueue) and taken from the front of the Queue using (Dequeue). A queue ultimately stores it’s elements in an array.

ReadOnlyCollection<T>

The ReadOnlyCollection class is for a generic read-only/immutable collection.

ReadOnlyDictionary<TKey, TValue>

The ReadOnlyDictionary is a class for generic key/value pairs in a read-only/immutable dictionary.

ReadOnlyObservableCollection<T>

The ReadOnlyObservableCollection is a class for a generic read-only/immutable observable collection. Changed may be made to the underlying observable collection but not to the ReadOnlyObservableCollection wrapper.

SortedDictionary<T>

The SortedDictionary is a dictionary where items are sorted on the key.

SortedList<TKey, TValue>

The SortedList is a collecion of key/value pairs that are sorted by key. A SortedList uses less memory than a SortedDictionary, but the SortedDictionary has faster insertion and removal of items.

SortedSet<T>

The SortedSet is a collection where items are maintained in sorted order. Duplicate items are not allowed.

Stack<T>

The Stack is standard LIFO (last-in first-out) collection. Items are pushed onto the top of the Stack and popped off of the top of the stack.

StringCollection

The StringCollection is a specialization of a standard collection for handling strings.

StringDictionary

The StringDictionary is a specialization of a standard dictionary for handling strings.

SynchronizedCollection<T>

The SynchronizedCollection is a thread-safe collection. It’s part of the System.ServiceModel assembly and used within WCF.

SynchronizedKeyedCollection<TKey, TValue>

The SynchronizedKeyedCollection is a thread-safe key/value type collection. It’s abstract, so is designed to be overloaded for your specific needs. . It’s part of the System.ServiceModel assembly and used within WCF.

SynchronizedReadOnlyCollection<T>

The SynchronizedReadOnlyCollection is a thread-safe read-only collection. This is part of the System.ServiceModel assembly.