There are six sets of collection classes, and they differ in how data is inserted, stored, and retrieved. Each of these classes is in the System.Collections.Generic namespace.
We start with the Collection Generic Class, which provides the base class for a generic collection:
Collection<string> dinosaurs = new Collection<string>();
dinosaurs.Add("Psitticosaurus"); dinosaurs.Add("Caudipteryx"); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Muttaburrasaurus");
The Collection class can be used immediately by creating an instance of one of its constructed types; all you have to do is specify the type of object to be contained in the collection. In addition, you can derive your own collection type from any constructed type, or derive a generic collection type from the Collection class itself.
The Collection class provides protected methods that can be used to customize its behavior when adding and removing items, clearing the collection, or setting the value of an existing item.
Most Collection objects can be modified. However, a Collection object that is initialized with a read-only IList object cannot be modified.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
Collection accepts a null reference as a valid value for reference types and allows duplicate elements.
This base class is provided to make it easier for you to create a custom collection. You are encouraged to extend this base class instead of creating your own.
List Collections: List<T>
The List<T> class has properties that make it behave similar to an array, the key difference being that it expands automatically as elements are added. List<T> can also shrink via calls to its TrimToSize() method or Capacity property. The corresponding non-generic class would be ArrayList.
Each element can be accessed by the index representing its position, e.g. List[6].
List<T> supports a Sort() method. Items must implement IComparable to be sortable. To search List<T>, you use the Contains(), IndexOf(), LastIndexOf() or BinarySearch() methods. BinarySearch() requires that the elements be sorted. You can also find multiple items with FindAll(), using a predicate parameter which returns a boolean.
Dictionary Collections: Dictionary<TKey, TValue>
Unlike List Collections, Dictionary classes store name - value pairs. The <name> parameter functions as a unique key that is used to look up it's corresponding value. The key can be any data type. The corresponding non-generic class is Hashtable.
You can add an item via the Add(key, value) method, or you can use the indexer: dictionary[key]=value. The indexer does not require an integer - the index type is specified by the first type parameter, TKey, when the Dictionary was created. An additional feature is the re-use of the same index. When you use dictionary[key]=value, instead of throwing an out of bounds exception, the Dictionary inserts a new element. If you then invoke dictionary[key]=value with the same key, the specified key already exists, so the Dictionary updates the existing element with the new value.
To deal with only the keys or elements in a Dictionary, you have the Keys and the Values properties. These return data type ICollection, which is a generic type. The data returned by these properties is a reference to the data in the original Dictionary, so changes therein are automatically reflected in the ICollection type returned by the Keys or Values properties.
Sorted Collections: SortedDictionary<TKey, TValue> and SortedList<T>
The sorted generic collection class elements are sorted by key for the SortedDictionary, and by value for SortedList. A foreach iteration returns the elements sorted in key order. With SortedList<T>, the Keys and Values properties return IList<TKey> and IList<TValue> respectively.
Stack Collections: Stack<T>
The Stack generic Collection is designed for last in, first out operations. It's two key methods are Push() and Pop(). Push() places elements into the collection; they do not have to be unique. Pop() retrieves -- and removes -- an element in the reverse order of how it was added. Just think of a "Stack" of plates. You always get the last one that was added off the top with Pop().
Queue Collections: Queue<T>
Queue Collections are identical to Stack Collections except that they follow a First in, First out pattern. The Queue<T> collection class behaves like a circular array. You place types into it at one end using the Enqueue() method, and you remove them from the other end with Dequeue(). As with Stack<T>, the objects do not have to be unique, and the class automatically increases in size as required. You can recover the capacity with TrimToSize().
Here at Eggheadcafe.com Dan Schwieg has an excellent article about the Blocking Queue, a non-generic implementation that can easily be converted to its generic counterpart.
Linked Lists: LinkedList<T>
The LinkedList<T> class enables both forward and reverse traversal with properties Next and Previous, and methods AddAfter(), AddFirst(), AddBefore(), AddHead(), AddLast() and AddTail(). LinkedList<T> has no non-generic counterpart.
You can find a lot more information about generic collection classes at MSDN. |