Generic collections were added in the .NET
Framework 2.0 and provide collections that are type-safe at compile time.
Because of this, generic collections typically offer better performance. Generic
collections accept a type parameter when they are constructed and do not require
that you cast to and from the
Object type when you add or remove items from
the collection. In the runtime Library, Generics are prefixed with
Generic. For example GenericList interface
represent in C# as List
GenericList Collections
This interface represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists. This is the generic equivalent of the ArrayList interface. GenericList accepts null as a valid value for reference types and allows duplicate elements. Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based. The GenericList is not guaranteed to be sorted. You must sort the GenericList before performing operations (such as BinarySearch) that require the GenericList to be sorted. In deciding whether to use the GenericList or ArrayList collections, both of which have similar functionality, remember that the GenericList class performs better in most cases and is type safe.
GenericDictionary Collections
This interface represents a collection of keys and values. It provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast. The following code example creates an empty GenericDictionary of strings with string keys and uses the Add method to add some elements. The example demonstrates that the Add method throws an ArgumentException when attempting to add a duplicate key. The example uses the Item property to retrieve values, demonstrating that a KeyNotFoundException is thrown when a requested key is not present, and showing that the value associated with a key can be replaced. The example shows how to use the TryGetValue method as a more efficient way to retrieve values if a program often must try key values that are not in the dictionary, and it shows how to use the ContainsKey method to test whether a key exists before calling the Add method. The example shows how to enumerate the keys and values in the dictionary and how to enumerate the keys and values alone using the Keys property and the Values property. Finally, the example demonstrates the Remove method.
GenericHashSet Collections
This interface represents a set of values. It provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order. The following example demonstrates how to merge two disparate sets. This example creates two GenericHashSet objects, and populates them with even and odd numbers, respectively. A third GenericHashSet object is created from the set that contains the even numbers. The example then calls the UnionWith method, which adds the odd number set to the third set.
GenericLinkedList Collections
This interface represent a doubly linked list. It is a general-purpose linked list. GenericLinkedList provides separate nodes of type GenericLinkedListNode, so insertion and removal are O(1) operations. You can remove nodes and reinsert them, either in the same list or in another list, which results in no additional objects allocated on the heap. The following code example demonstrates many features of the GenericLinkedList interface.
GenericStack Collections
This interface represents a variable size last-in-first-out (LIFO) collection of instances of the same specified type. Stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use GenericQueue if you need to access the information in the same order that it is stored in the collection. Use GenericStack if you need to access the information in reverse order. GenericStack accepts null as a valid value for reference types and allows duplicate elements. The following code example demonstrates several methods of the GenericStack interface. The code example creates a stack of strings with default capacity and uses the Push method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The Pop method is used to pop the first string off the stack. The Peek method is used to look at the next item on the stack, and then the Pop method is used to pop it off.