Thursday, October 16, 2008

When Hashtable is better than Dictionary

We all know Hashtable class - it exists in .NET from the very beginning. .NET 2.0 has brought a generic replacement to it: Dictionary. So we usually prefer using it rather than Hashtable. But it isn't well known Hashtable has a very nice feature related to concurrent usage, which Dictionary does not have (at least by documentation):

- Hashtable supports single writer and multiple readers concurrent access policy
- But Dictionary - standard single writer or multiple readers concurrent access policy

This means usage of Hashtable is perfect e.g. in concurrent caching scenarios: only write operations on it require locking to ensure thread safety, but in case with Dictionary both read and write operations require locking.

The official "approval" of this fact is here.

And a fly in the ointment:
- It's difficult to find out if this was fully correct for .NET 2.0, since this fact was mentioned only recently. The issue report related to this is here.
- This is not completely true: here is the report.

Possible workarounds:
- Never clear or remove items from Hashtable at all. In many cases this is ok, e.g. when you cache something for a lifetime of the cache or the whole application. An example is caching of Reflection.Emit results or any other code loaded into AppDomain: you anyway can't unload the code, so there is no reason to clean up any references to it from caches.
- Implement item removal by dropping the old Hashtable and copying its content (without the item to remove) to a new one. Or implement a wrapper doing the same using two Hashtables (one contains just added keys and values, and andother one just removed keys) and periodically doing the garbage collection be re-creating both of them as described above. But actually the cost of looking into two Hashtables must be close to the cost of locking.

In any case, this feature is very nice even with all the mentioned problems. We utilize this feature of Hashtable e.g. in our ThreadSafeDictionary class.


  1. I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information.
    Download From Warez

  2. This is a great web site. Good sparkling user interface and very informative blogs. I will be coming back in a bit, thanks for the great article. I have found it enormously useful. downlodable driver

  3. If you have a lot of work that needs currency conversion, multiple currencies together with excel, this is a great tool. Peter has a reputation for &'ratting&' his collegues out in times of trouble, so if you&'re gonna break policy, don&'t let Peckerhead see you.

  4. You can now access Magic Pencil using your Facebook or Google+ Email account. link for you Little Monster at School Wanderful interactive storybook in UK English and French for iPhone.

  5. When her poetry failed togain recognition, she produced her first and best known novel, TheLeavenworth Case (1878). Added the top independent coffeehouses in ParisUpdated and optimized for iOS 8, iPhone 6 and iPhone 6 PlusNumerous bug fixes.

  6. Never miss an activity because you dont have a ride. downlodable pdf We are a leader in education innovationcreating award-winning toys and reading and mobile learning systems that make play and learning even more fun.

  7. - Quick reference math tables for counting, addition, subtraction, and multiplication. This page. Do you want to check out the weather for your city quickly without having to enter zip codes or navigating a complicated application?

  8. From iJour: Studying the life of aristocratic families in Tibetan history is a rather difficult topic for Tibetologists. link for you 6) The Feed is meant to be customized to your liking.

  9. Most actions have a template you can use to configure how the data will look when it's passed forward. concerning hobbits piano sheet music pdf Auto sync option to auto sync every newly created business card to cloud.

  10. We've provided info/help buttons for each sub-step. Die App sortiert auf Wunsch auch nach Schwierigkeit.