'Anil' Radhakrishna's Code Gallery

Home | Blog | Contact
Pros & Cons of different Collections in C#

Type

Description

ArrayList

This is a dynamically sizable array. It is useful when you do not know the required array size at design time.

Avoid ArrayList for storing strings.

Use ArrayList to bind the read-only sorted data to a data grid as a data source.

Use ArrayList for zero-based index access to the data.

Hashtable

This is a collection of key/value pairs that are organized based on the hash code of the key. It is appropriate when you need to search but not sort.

Hashtable is suitable for large number of records.

HybridDictionary

This uses a ListDictionary when the collection is small, and switches to Hashtable when the collection gets large.

Use HybridDictionary for frequently queried data.

Do not use HybridDictionary to sort data. It is not optimized for sorting.

ListDictionary

This is useful for storing 10 or less key/value pairs.

NameValueCollection

This is a sorted collection of associated String keys and String values that can be accessed either with the key or with the index.

Use NameValueCollection for sorting strings.

Use NameValueCollection to store strings of key/value pairs in a pre-sorted order.

Use NameValueCollection for frequently changing data.

Queue

This is a first-in, first-out collection that implements ICollection.

Use Queue when you need to access data sequentially, based on priority.

SortedList

This is a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.

Use SortedList for fast object retrieval using an index or key.

Use SortedList for sorting data that is mostly static and needs to be updated only infrequently.

Avoid using SortedList for large data changes because the cost of inserting the large amount of data is high.

The SortedList is a dictionary class that supports sorting.

Stack

This is a simple last-in, first-out collection of objects.

Use Stack where you can discard the items after processing it.

Use Stack where you do not need to access arbitrary items in the collection.

Specify the initial capacity if you know the size.

StringCollection

This is a strongly typed array list for strings.

Use StringCollection for zero-based index access to the data.

Use StringCollection to store string data that changes frequently and needs to be retrieved in large chunks.

Do not use StringCollection for sorting strings or to store presorted data.

StringDictionary

This is a hash table with the key strongly typed to be a string rather than an object.

Always prefer StringDictionary over Hashtable for storing string key/value pairs if you want to preserve the string type to ensure type safety.

OrderedDictionary

Use the OrderedDictionary to store name/value pairs in a way that preserves the order of adding them to the collection.

When you need a fast dictionary but also need to keep the items in an ordered fashion.

BitArray

The real power of the BitArray is in its ability to perform Boolean operations on two BitArray objects (of the same size).

BitVector32

It is fixed at 32 bits. It deals with sets of Boolean values.

The BitVector32 is not a resizable collection at all.

BitVector32 stores all its data as a single 32-bit integer.



References:
Collections Explained
MCTS Self-Paced Training Kit (Exam 70-536): Microsoft .NET Framework 2.0 Application Development Foundation