Choose List, Choose a Job, Choose a Career …
There are two types of collections: generic and non-generic. A non-generic collection is one which can contain any data type, whereas the generic collection must state the type as a parameter. Generics can be considered safer to use as you have greater control of the data involved. Microsoft themselves recommend the following:
“Be sure to choose your collection class carefully. Using the wrong type can restrict your use of the collection. In general, avoid using the types in the System.Collections namespace unless you are specifically targeting .NET Framework version 1.1. The generic and concurrent versions of the collections are to be preferred because of their greater type safety and other improvements.”
Here is a brief explanation of some of the more commonly used Generic Collections. This is not an extensive list.
Generic lists are ideal for storing and retrieving large number of elements. You must specify the data type <T>. As you add entries the size of the list will expand – it is not fixed.
The data can have nulls and duplicates and supports indexing. Lists are not guaranteed to be in order, so the Sort() method may be necessary.
A collection of key-value pairs where the data type of each is specified. The key is used to identify and order each pair. It acts as primary key so it must be unique and cannot be null. The values can be null or duplicates.
A SortedList is quite similar to a Dictionary, although the SortedList uses less memory.
It is a collection of key-value pairs that are sorted by the key in ascending order. When a new value is inserted, it is automatically placed in the correct order and the indexing adjusts accordingly. The key must be unique and cannot be null; the value can be null or duplicate. You can also have a non-generic SortedList which can take any data type.
Similarly, a Stack may be generic or non-generic. Stack<T> can be described as a last-in-first-out (LIFO) non-generic collection of objects that are of the same specified type. Use a Stack if you wish to access data in a reverse order.
A first-in-first-out (FIFO) non-generic collection of objects. A Queue can be used to access data in the same order as it is entered in. Both Stacks and Queues are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value.
Each collection type has its own strengths and weaknesses, but which to use? It really comes down to what the situation calls for:
- If you need to access the elements in a certain order, you should choose to use a Queue<T> for FIFO or a Stack<T> for LIFO.
- A Dictionary<TKey, TValue> or List<T> would be suitable if you needed to access each element by an index.
- If you need to sort the elements differently from how they were entered, you could you a SortedList<T>. A List<T> also has a Sort() method which could also have the desired effect.
Be aware that there are many other types of Generic Collections which may better suit your needs:
- KeyedCollection< TKey,TValue >