Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Hashtable objects still useful?

Tags:

c#

collections

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

like image 500
jro Avatar asked Oct 19 '09 17:10

jro


2 Answers

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.

like image 200
LBushkin Avatar answered Sep 28 '22 07:09

LBushkin


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 :)

like image 45
JaredPar Avatar answered Sep 28 '22 05:09

JaredPar