Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need of OrderedDictionary, ListDictionary, and HybridDictionary?`

What is the need of three different dictionaries- OrderedDictionary,ListDictionary and HybridDictionary when all of them perform similar functions?

None of them is sorted, and elements of the collection can be retrieved by key in all of them. So, what is the purpose of three different classes?

like image 657
Batrickparry Avatar asked Jan 07 '11 06:01

Batrickparry


2 Answers

In a nutshell:

  • Dictionary - Well, a dictionary.

  • ListDictionary - Used for small collections, typically less than 10 items

  • HybridDictionary - Used when the collection size is unknown (switches implementations depending on the size of the collection)

  • OrderedDictionary - The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary<TKey, TValue> class. You can access elements either by the key or by the index.

like image 78
Kyle Rosendo Avatar answered Nov 15 '22 15:11

Kyle Rosendo


To complement Kyle's answer:

OrderedDictionary allows retrieval by key and index (it uses a hashtable and and array internally) but has a bigger overhead per item

ListDictionary has a linked list as its internal structure, it does not perform well for insertion and retrieval by key but preserves the original insert order

HybridDictionary is a ListDictionary if the dictionary does not contain many items and converts to a Hashtable if the number of items reaches a speficic limit (I personally think you should use Dictionary<,> instead of it since C#2)

like image 40
vc 74 Avatar answered Nov 15 '22 16:11

vc 74