
One powerful capability in the .NET Framework and C# language is the support for generic types. These types, mainly found in the System.Collections.Generic namespace, are polymorphic based on their type parameters (parametric polymorphism). The types in System.Collections.Generic can provide amazing performance and simplicity in your source code.
Implementation classes
Comparer<>
Dictionary<>
EqualityComparer<>
HashSet<>
KeyValuePair<>
LinkedList<>
LinkedListNode<>
List<>
Queue<>
SortedDictionary<>
SortedList<>
SortedSet<>
Stack<>
Interfaces
ICollection<>
IComparer<>
IDictionary<>
IEnumerable<>
IEnumerator<>
IEqualityComparer<>
IList<>
ISet<>
Exceptions
KeyNotFoundException
Dictionary, IDictionary. In the .NET Framework, a Dictionary is a lookup table that allows objects as keys and objects as values. It is typically implemented with hash code lookups, making it extremely fast to search for elements.
Dictionary Examples IDictionary Generic Interface Dictionary ExamplesComparer, EqualityComparer, IComparer, IEqualityComparer. The Comparer and EqualityComparer interfaces are contracts that require methods to compare two objects. The difference is that Comparer requires a method Compare() that will tell which object is greater or less than the other. EqualityComparer, on the other hand, only needs to determine if two objects are considered equal.
IEqualityComparer Dictionary
HashSet, SortedSet, ISet. The HashSet and SortedSet implement set logic. This means that only one element with a certain value can be present in the collection, and you can use set operations such as Union or Overlaps to mutate the set. The ISet interface defines common functionality in the HashSet and SortedSet. The HashSet is typically faster for programs that need frequent lookups.
HashSet Programs SortedSet Examples
KeyValuePair. This type is implemented as a struct; it simply stores two references to objects. You can store any type in the key and any type in the value slot. Another type that is similar to the KeyValuePair<> type is the Tuple type.
KeyValuePair Hints Tuple TipsLinkedList, LinkedListNode. Linked lists are collections that allow for fast insertion and removal of elements anywhere in the list. For most other purposes, though, they are slower than a regular List. In my experience, the LinkedList type is not very popular, and it is often more trouble than it is worth.
LinkedList Programs
List, IList. The List type is a fantastic resizable array data structure. You can append elements to its end very quickly, and it contains internal logic to avoid excessive resizes. Adding elements to the start is slow, however, and you cannot do very fast lookups. I recommend the List most often.
List Examples IList Generic Interface List TipsQueue. The Queue type is not often useful, but when you need to implement a history mechanism which stores the last several items used, it can be useful. It does not really provide any performance advantages over other implementations. It can make for simpler code in some cases.
Queue ClassSortedDictionary. The SortedDictionary is much slower for lookups than a Dictionary, but it always has a sorted representation of its internal elements. If having a sorted collection is most important to you, consider this collection. If you have any concern about lookup performance, or don't want to have that concern in the future, prefer Dictionary.
SortedDictionarySortedList. The SortedList is another always-sorted collection. If you have a List and are always sorting the List before displaying it or persisting it, consider the SortedList to make your code simpler.
SortedList CollectionStack. The most famous use for a Stack is the implementation of parsers. When parsing HTML, you can push an element to the top of the stack, and when that element closes, you can pop the element from the top of the Stack.
Stack CollectionIEnumerable. The IEnumerable interface allows you to loop over a variable in a foreach-loop. As each iteration of the foreach-loop is reached, the IEnumerable interface can execute arbitrary code. This can improve performance in some programs because work that is not needed is not done.
IEnumerableIEnumerator. The IEnumerator interface is implemented on generic collections and allows users to loop over all the elements in the foreach-loop. The IEnumerator interface is part of other generic collections, while the IEnumerable interface can be used directly to support looping.
KeyNotFoundException. The KeyNotFoundException is not a generic type, but it is found in the System.Collections.Generic namespace. It represents an error that occurs when trying to access a key that does not exist in a Dictionary or similar (IDictionary-based) collection.
KeyNotFoundException Fix
The .NET Framework contains other generic types that are not present in System.Collections.Generic, but the ones here are some of the most popular and core to the framework. A solid understanding of these types can benefit your programs and also save you time and the hassle of implementing custom data structures when none are needed.
Collections