tags:

views:

211

answers:

2

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#.

+1  A: 

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

  • You want to get the previous semantic of an IDictionary instance returning null vs. throwing in the case a key is not present in the table.
  • Dealing with a legacy API which exposes a Hashtable value

Hashstable is still useful as a general purpose map if you are stuck with C# 1.0 :)

JaredPar
Thanks Jared, I should update my question to reflect C#, specifically.
jro
The null/throw scenario is one that slipped my mind. I still mentally prefer the syntax to that approach vs. the generic Contains(key) type of structure, even though best practices seem to say otherwise.
jro
+9  A: 

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.

LBushkin
Ahh, COM Interop. Good point.
jro