.NET Collections
Sometimes, the proper choice of a collection can greatly impact the performance of your application. For example, there are collection types that are more appropriate for insertions, others that allow faster lookups, and so on. Plus, you must decide if you want indexed collections or not, and if you want to have generic collections, in order to have compile-time type checking. The .NET BCL comes with several general-purpose collection classes. Here is a list of all the collection classes in .NET 3.5 SP1 and their intended use.
| Purpose |
Types |
Description |
| Fixed size |
System.Array |
Can have multiple dimensions |
| FIFOs (Queues) |
System.Collections.Queue, System.Collections.Generic.Queue<T> |
|
| LIFOs (Stacks) |
System.Collections.Stack, System.Collections.Generic.Stack<T> |
|
| Linked lists |
System.Collections.Generic.LinkedList<T> |
Random inserts and deletes |
| Array-based |
System.Collections.ArrayList, System.Collections.Generic.List<T> |
|
| Customizable |
System.Collections.ObjectModel.Collection<T> |
can override InsertItem, RemoveItem, SetItem and ClearItems to implement custom algorithms |
| Thread-safe |
System.Collections.Generic.SynchronizedCollection<T>, System.Collections.Generic.SynchronizedReadOnlyCollection<T> |
|
| Read-only |
System.Collections.ObjectModel.ReadOnlyCollection<T>, System.Collections.Generic.SynchronizedReadOnlyCollection<T>, System.Collections.ObjectModel. ReadOnlyObservableCollection<T> |
Wrap existing (not read-only collections) |
| Sorted |
System.Collections.SortedList, System.Collections.Generic.SortedList<K,V>, System.Collections.Generic.SortedDictionary<K,V> |
|
| Sets |
System.Collections.Generic.HashSet<T> |
no repetition of elements |
| Keyed |
System.Collections.Specialized.ListDictionary (for <=10 items), System.Collections.Generic.Dictionary<K,V>, System.Collections.Hashtable (index by hash), System.Collections.Generic.KeyedByTypeCollection<T> (index by type), System.Collections.Specialized.HybridDictionary (changes depending on number of elements, ListDictionary -> Hashtable), System.Collections.Specialized.OrderedDictionary (access by key or index) |
|
| Multiple values for a single key |
System.Collections.Specialized.NameValueCollection |
|
| Bits |
System.Collections.BitArray, System.Collections.BitVector32 (up to 32 bits only) |
|
| Strings |
System.Collections.Specialized.StringDictionary, System.Collections.Specialized.StringCollection |
|
| Observable |
System.Collections.ObjectModel.ObservableCollection<T>, System.Collections.ObjectModel. ReadOnlyObservableCollection<T> |
Identical to System.Collections.ObjectModel.Collection<T>, fires events upon adding, removing, modifying or clearing |
Of course, you should not expose a collection class directly, instead you should use collection interfaces, that all these classes implement (at least, one of them). These interfaces are:
| Purpose |
Types |
| Enumerate, count |
System.Collections.ICollection, System.Collections.Generic.ICollection<T> |
| Indexed access, add/remove |
System.Collections.IList, System.Collections.Generic.IList<T> |
| Keyed |
System.Collections.IDictionary, System.Collections.IOrderedDictionary, System.IDictionary<K,V> |
| Enumerate only |
System.Collections.IEnumerable, System.Collections.IEnumerable<T> |
There are some useful constructs missing, most notably, ISet and ISet<T>, however, a set implementation exists, as of .NET 3.5: HashSet<T>. Some recommendations for using collections:
- Collections that rely on object identity may need proper implementation of Equals or GetHashCode methods
- For customized object equality comparison, a custom implementation of System.Collections.IEqualityComparer<T> or System.Collections.IEqualityComparer may be passed on the constructor
- For sorted collections, a custom implementation of System.Collections.IComparer<T> or System.Collections.IComparer may be passed on the constructor (System.Collections.Comparer, System.Collections.CaseInsensitiveComparer are default implementations)
- If number of elements is known beforehand, use the constructor that specifies the initial capacity
- Avoid using value types, except for keys
And that's it.