3

I'm studying the .NET Hashtable class, and would like to experiment with various aspects...

  • How many collisions (rehashes) are occurring in a given dataset
  • Is the expansion of the hashtable safe for multithreaded access?
  • What is the current load factor of my hashtable?

How can I experiment or learn the above information regarding a given hashtable or dictionary

1 Answer 1

2

The thread safety of Hashtable is stated in MSDN. It is thread safe if only one thread writes to the Hashtable, and this has to include expansion.

Now, some digging using Reflector:

The load factor is stored in the loadFactor private field, which you can access using reflection if you want to check its value.

Rehashes are harder. These is no internal state of the Hashtable that is modified in a detectable way by a rehash and only by a rehash. Thus you would have to look at other options here, for example using Reflector to create your own identical Hashtable implementation and then adding some code to count rehashes. If you're doing that, you may as well expose the load factor as a property as well to make your life easier.

Sign up to request clarification or add additional context in comments.

3 Comments

What else would modify the internal state other than a rehash? If I observe my own interactions with the Hashtable, would that be sufficient to deduce whats happening? ...I'll try using Reflector to answer this and will post the results here.
The link seems to indicate multiple writes are supported via Synchronized.
@makerofthings7, would You be so kind to post your findings here, please?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.