Wednesday, October 25, 2017

HashTable vs ListDictionary vs HybridDictionary

HashTable:
Hashtable is container which is recommended to work with large number of key value data objects. It’s deprecated because of dictionary class which is introduced in .Net 2.0. Because does not need to box or unbox data.  Dictionary is a generic type, Hashtable is not. That means you get type safety with Dictionary, because you can’t insert any random object into it, and you don’t have to cast the values you take out.
ListDictionary:
If, for example, you are dealing with a small number of items, you might not want to use a Hashtable since there are some noted inefficiencies in hasthtable. If you know the collection size is less than 10 items, recommended to use ListDictionray.
HybridDictionary:
HybridDictionary comes to optimize Hashtable.
If number of key value item storing in collection is less,  prefer ListDictionary. Some situation we are not sure about size we can go with Hybrid dictionary. HybridDictonary plays as ListDictionary while the item size is less if it grows the item  it will automatically switched from ListDictionary to Hashtable.
Many will ask the question “why not just use a HybridDictionary all the time?”
There is overhead associated with HybridDictonary because it has to constantly monitor the size while the size is increase/decrease need to convert between ListDictionary to Hashtable as needed.
Conclusion of the story is use
ListDictonary: if collection is small
HashTable : if collection is large
HybridDictonary: Is recommended while the number of elements is unknown


No comments:

Post a Comment