Using Generic Collections

In the recent posts, I’ve constantly referred to generics. The concept of generics is to simply provide classes (collections) with placeholders for types, and let them work with the given types. In this manner, type-safety can be enforced, and performance will be increased by evading the constant converts from a type to another.

Let’s consider the following example: we have a List collection, and we’d like to assign strings for it. But nothing prevents us from assigning for example an Exception, as a mistake. Even worse, we can create a useless pile of junk from our List collection (there is no reason why we should, the key point here is that we can). Now, by using the List<string> generic class, we can rest assured that our collection can only contain strings. It is beautiful, and even better, it works with everything, even your custom classes.

That’s enough for introduction, let’s get back to business. Generic collections live in the System.Collections.Generic namespace. This namespace provides the classes that can be found in the System.Collections namespace, with the addition of type placeholders. The standard collection interfaces have generic counterparts, so let’s start with them:

Generic Interfaces
ICollection <T> Provides the following typed methods: Add, Clear, Contains, CopyTo, Remove.
IComparer <T> Compares an instance of the implementing class with another instance of the same type.
IDictionary <K, V> Provides a dictionary with K as the key type and V as the value type.
IEnumerable <T> Provides the typed GetEnumerator method.
IEnumerator <T> Implements the IEnumerator interface. Adds a generic Current property.
IEqualityComparer <T> Provides a hash-based equality test in two instances of the same type.
IList <T> Provides methods to work with a strongly-typed list.

 

Dictionary <K, V>

The generic Dictionary class provides a strongly-typed collection of key-value pairs. Items can be retrieved using their keys. The underlying storage is a Hashtable. Implements the generic IDictionary, ICollection, IEnumerable interfaces, and their non-generic counterparts.

Comparer <T>

The generic Comparer class provides a base class for the implementations of the generic IComparer interface. If the type T not implements the generic IComparer interface, Comparer <T> falls back to the non-generic IComparer interface.

KeyValuePair <K, V>

The generic KeyValuePair struct defines a key-value pair (K for key and V for value) that can be set or retrieved. When you work with a foreach iterator, remember that the generic Dictionary class stores generic KeyValuePair structs. So you should use the KeyValuePair struct to iterate over the collection, instead of using the type of the key or the value.

List <T>

Provides a strongly-typed collection of objects where items can be accessed by indexes. Implements generic and non-generic IList, ICollection, IEnumerable interfaces. The generic List is the generic implementation of the ArrayList class.

Queue <T>

Provides a first-in-first-out (FIFO) collection of strongly-typed objects. Use the Enqueue method to add a new item, the Dequeue to retrieve the first one, and the Peek to retrieve the first item without removing it from the Queue.

Stack <T>

Provides a first-in-last-out (LIFO) collection of strongly-typed objects. Use the Push method to add a new item to the stack, the Pop to retrieve the last inserted item, or the Peek to retrieve the last item without removing it from the Stack.

SortedDictionary <K, V>

Represents a strongly-typed dictionary which can be sorted by the key. The values can be retrieved as SortedDictionaries, by the key.

LinkedList <T>

Is a doubly linked strongly-typed list. Implements the ICollection interface and supports enumerators. The items stored in a LinkedList are LinkedListNodes, each one has a Next and a Previous node. It also expose two properties, First and Last (the node).

 

Further Readings

Dictionary <K, V>

Comparer <T>

KeyValuePair <K, V>

List <T>

Queue <T>

SortedDictionary <K, V>

LinkedList <T>

Advertisements

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: