Hashtable vs. Dictionary
-
when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();
-
when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();
a Dictionary is a generic collection where as a Hashtable is just a collection. So if you need to use generics use a Dictionary, if you don't use a Hashtable. Otherwise, they are identical, think of a Dictionary as a Hashtable for generics, just like ArrayList and List. btw, should be: Dictionary<typeOfKey, typeOfValue> cacheLanguage = new Dictionary<typeOfKey, typeOfValue>(); -- modified at 14:53 Monday 24th July, 2006
-
when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();
Because a HashTable uses object for both the key and value it is possible to do soemthing like this
Hashtable ht = new Hashtable(); ht.Add("one", 1); ht.Add(1, 1);
Not very type safe and it incurs the boxing/unboxing penalty. Where as the Dictionary uses the specified types for key and value the above would not be possible (unless you used object as the key)only two letters away from being an asset
-
Because a HashTable uses object for both the key and value it is possible to do soemthing like this
Hashtable ht = new Hashtable(); ht.Add("one", 1); ht.Add(1, 1);
Not very type safe and it incurs the boxing/unboxing penalty. Where as the Dictionary uses the specified types for key and value the above would not be possible (unless you used object as the key)only two letters away from being an asset
So basically the two does exactly the same thing, but Dictionary is just type safe? Does a Dictionary also hash the key when storing key/value pairs? They say Hashtables are optimized for fast retrievals and that is what I need for a multi-lingual website.
-
Because a HashTable uses object for both the key and value it is possible to do soemthing like this
Hashtable ht = new Hashtable(); ht.Add("one", 1); ht.Add(1, 1);
Not very type safe and it incurs the boxing/unboxing penalty. Where as the Dictionary uses the specified types for key and value the above would not be possible (unless you used object as the key)only two letters away from being an asset
Mark Nischalke wrote:
ht.Add("one", 1); ht.Add(1, 1);
:wtf: ;P
**
xacc.ide-0.2.0 preview - Now in 100% C# goodness
**
-
when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();
If you have very few items a hashtable is the fastest. A Dictionary is still a key value pair and boxes unless you use a generic. Dictionaries are easier to use than a hashtable. But, since you will be storing many words or something, then a dictionary would be more useful to store unique sets. But, then again thats why globalization namespace was made.
1 line of code equals many bugs. So don't write any!!
-
when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();
Dictionary class is implemented as a hash table and it's a generic class and performance wise it's close to O(1). Therefore I'm not sure why would one use hash table unless for backward compatability. More here: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/T_System_Collections_Generic_Dictionary`2.htm Regards.