Has the System.Collections.Hashtable object become obsolete?
With the implementation and refinements in generics from C# v2 and v3, it's been a long time since I've found a Hashtable more appropriate than a generic Dictionary. Literally, I can't recall the last time I used Hashtable.
Just wondering if anyone else has found a case where a Hashtable is still appropriate or preferred for implementation and the basis for that decision -- ease of use, performance, collection size, object type, etc.
UPDATE: was meaning to limit this question to C#.
Aside from the obvious case where you must use a Hashtable in an existing API that expects one (such as languages that don't yet support generic types). You also may need to use them if you need to access them from COM. I believe that Hashtable is COM visible, whereas Dictionary is not.
Other than that, you generally want to use Dictionary<> because it avoids boxing (a performance hit) and provides type safety.
Hashtable's are still useful in the cases where you are dealing with a language, or version of a language, which does not support generic types.
EDIT
For C# 2.0 or higher, Hashtable
is really only of useful for the following scenarios
IDictionary
instance returning null vs. throwing in the case a key is not present in the table. Hashtable
valueHashstable is still useful as a general purpose map if you are stuck with C# 1.0 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With