Collections – short comparison

Dictionary:
Containst pairs (key and value).
Items must correctly implement Equals() and GetHashCode() or IEqualityComparer must be passed in constructor.
Is fast for lookup and inserts.
Cannot be easily traversed in order (no sorting).
Implementation is hashtable.

SortedDictionary:
Containst pairs (key and value).
Key items must implement correctly IComparable.
Implementation is BinaryTree.
A little bit slowler than normal dictionary, but items are sorted.

List:
Contains items without keys.
Reading items by index is quick.
Removing or adding in the middle of is slow.
Implementation using array.
Is not sorted.

LinkedList:
Contains items without keys.
Adding and removing is quick.
Reading items directly by index is not possible, but in order is fast.

HashSet:
Contains unique items.
Fast lookup and adding, but no ordering.
Items must correctly implement Equals() and GetHashCode() or IEqualityComparer must be passed in constructor.

Stack:
Implementation of LIFO.
Fast lookup and adding, but only one element is accessible.

Queue:
Implemenatation of FIFO.
Fast lookup and adding, but only one element is accessible.

If you need thread-safe collection you should use concurrent collections from System.Collections.Concurrent namespace.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s