Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7497501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:07:26+00:00 2026-05-29T19:07:26+00:00

I am trying to understand how Hashtables work in C#. I read the MSDN

  • 0

I am trying to understand how Hashtables work in C#. I read the MSDN article and I understand that C# Hashtables use ‘rehashing’ for collisions, i.e. if I try to insert a key/value pair into the hashtable, if using HashFunction H1 results in a collision, then it will try HashFunction H2, H3, etc, until no collisions are found.

MSDN quote:

The Hashtable class uses a different technique referred to as
rehasing. (Some sources refer to rehashing as double hashing.)

Rehashing works as follows: there is a set of hash different
functions, H1 … Hn, and when inserting or retrieving an item from
the hash table, initially the H1 hash function is used. If this leads
to a collision, H2 is tried instead, and onwards up to Hn if needed.
The previous section showed only one hash function, which is the
initial hash function (H1). The other hash functions are very similar
to this function, only differentiating by a multiplicative factor. In
general, the hash function Hk is defined as:

Hk(key) = [GetHash(key) + k * (1 + (((GetHash(key) >> 5) + 1) %
(hashsize – 1)))] % hashsize

However, taking the example from the MSDN site1:

private static Hashtable employees = new Hashtable();

public static void Main()
{
    // Add some values to the Hashtable, indexed by a string key
    employees.Add("111-22-3333", "Scott");
    employees.Add("222-33-4444", "Sam");
}

Let’s assume that adding the second key will result in a collision, so H2 will have to be used. However, when I call employees[“222-33-4444”], how does the hashtable know to use H2? Is there a separate mapping? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:07:28+00:00Added an answer on May 29, 2026 at 7:07 pm

    Hash tables store both the key and the value in the hash table itself. This way later on during operations such as hash table look-ups it can be guaranteed that the value found is the one that matches the index used for the look-up. Hash tables use a simple “try the basic method of look-up until success” methodology. In this case, the method of look-up is “use hash function X” where X changes on failure.

    In other schemes, the method of look-up is “look at the table entry X” (as determined by a hash function) where X just increases by one in a wrapping manner each failure.

    The nagging question now is what happens when the value ISN’T in the table? Well, that can be rather ugly: When you’ve either hit an entry in the table which is missing or, even worse, when you’ve iterated through as many entries as are stored in the table, you can be sure the entry isn’t there — but that can take “a while” in the worst case.

    Keep in mind that since only one value can be associated with one key, once you’ve found the key, you’ve found the value. The worst a hash table can do is having to do the equivalent of a cache-unfriendly linear search over all the values in the hash table itself… but ultimately, it will find the value if it’s there because it’s comparing the stored key to the requested key to test if it’s there. The only optimization closed hash tables make is where to look first — in this case, where hash function 1 says, and then 2, and then 3…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to understand how Ruby blocks work, and to do that I've
I'm trying understand how try ... catch construction works in T-SQL. So I've read
I am trying understand how multi queries work in mysqli. But I confess that
I am trying to understand what the object.GetHashCode() is used for. I read that
Trying to understand why you can't use sql_variant for the value type when using
Just trying to understand that - I have never used it before. How is
I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is
Trying to understand something that I don't know how to describe because I don't
Trying to understand why this doesn't work. I keep getting the following errors: left
Im trying to understand how boolean logic works when I use NOT. To give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.