tags:

views:

10931

answers:

6

Duplicate of: Why Dictionary is preferred over hashtable in C#?


What is the difference between Dictionary and Hashtable. How to decide which one to use?

+20  A: 

Simply, Dictionary<TKey,TValue> is a generic type, allowing:

  • static typing (and compile-time verification)
  • use without boxing

If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections)

Marc Gravell
+1 for prefer :)
ck
Thank you very much Marc!
Mahatma
But Dictionary's instance methods aren't thread-safe, unlike the Hashtable
t3mujin
+4  A: 

Dictionary is typed (so valuetypes don't need boxing), a Hashtable isn't (so valuetypes need boxing). Hashtable has a nicer way of obtaining a value than dictionary IMHO, because it always knows the value is an object. Though if you're using .NET 3.5, it's easy to write an extension method for dictionary to get similar behavior.

If you need multiple values per key, check out my sourcecode of MultiValueDictionary here: http://stackoverflow.com/questions/380595/multimap-in-c-3-0/380601#380601

Frans Bouma
For multiple values per key: in .NET 3.5, you might also consider implementing `ILookup<TKey,TValue>` (which is the multi-map interface). Unfortunately the default concrete implementation is immutable, but it is easy to re-implement (or add to your MultiValueDictionary). There is a simple example of such in MiscUtil (EditableLookup<TKey,TValue>)
Marc Gravell
Good tip indeed, I had forgotten about that interface. I looked at the implementation in the BCL but it's indeed immutable so pretty much useless for every-day multi-value usage ;). I'll add the interface.
Frans Bouma
Done. Republished code:http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx
Frans Bouma
Hmm, there's a problem with ILookup<>: it also implements an Enumerator, which is different from the Dictionary enumerator. When a Linq operator is used on the multivaluedictionary, it can't chose which enumerator to use as it also can use IEnumerable<TKey, IGrouping<TKey, TValue>>, even though ILookup is implemented explicitly... Which makes it harder to use the dictionary as it requires explicit type specfication.
Frans Bouma
MultiDictionary is also present in PowerCollections: www.wintellect.com/powercollections.aspx
Dmitri Nesteruk
+1  A: 

hopefully answers to this question provides you a good answer

TheVillageIdiot
+1  A: 

The Hashtable class is a specific type of dictionary class that uses an integer value (called a hash) to aid in the storage of its keys. The Hashtable class uses the hash to speed up the searching for a specific key in the collection. Every object in .NET derives from the Object class. This class supports the GetHash method, which returns an integer that uniquely identifies the object. The Hashtable class is a very efficient collection in general. The only issue with the Hashtable class is that it requires a bit of overhead, and for small collections (fewer than ten elements) the overhead can impede performance.

Rashmi Pandit
My comments are from MCTS 2.0 book and its gotten -ve rep ... how ironic!!!! :D
Rashmi Pandit
Not really very ironic... both Hashtable and Dictionary<,> are based on this approach, so it doesn't in any way answer the question of choosing between them.
Marc Gravell
The real irony is that I was reading the same MCTS book, got really confused as to which one to prefer, posted it here and got the same text i just read in the form of your answer!!! :)Thanks for replying nyways..
Mahatma
Ha ha ha ha ... we are dealing with lot of ironies (is tht how its spelled) today. Anyways, thanks for clarifying Marc :)
Rashmi Pandit
lol at the meta-irony
Marc Gravell
+1  A: 

There is one more important difference between a HashTable and Dictionary. If you use indexers to get a value out of a HashTable, the HashTable will successfully return null for a non-existent item, whereas the Dictionary will throw an error if you try accessing a item using a indexer which does not exist in the Dictionary

rbg
A: 

ILookup Interface is used in .net 3.5 with linq.

The HashTable is the base class that is weakly type; the DictionaryBase abstract class is stronly typed and uses internally a HashTable.

I found a a strange thing about Dictionary, when we add the multiple entries in Dictionary, the order in which the entries are added is maintained. Thus if I apply a foreach on the Dictionary, I will get the records in the same order I have inserted them.

Whereas, this is not true with normal HashTable, as when I add same records in Hashtable the order is not maintained. As far as my knowledge goes, Dictionary is based on Hashtable, if this is true, why my Dictionary maintains the order but HashTable does not?

As to why they behave differently, it's because Generic Dictionary implements a hashtable, but is not based on System.Collections.Hashtable. The Generic Dictionary implementation is based on allocating key-value-pairs from a list. These are then indexed with the hashtable buckets for random access, but when it returns an enumerator, it just walks the list in sequential order - which will be the order of insertion as long as entries are not re-used.

shiv govind Birlasoft.:)

shiv govind