Why is Dictionary preferred over Hashtable in C#?

后端 未结 19 1511
长情又很酷
长情又很酷 2020-11-22 05:53

In most programming languages, dictionaries are preferred over hashtables. What are the reasons behind that?

19条回答
  •  深忆病人
    2020-11-22 06:34

    The Extensive Examination of Data Structures Using C# article on MSDN states that there is also a difference in the collision resolution strategy:

    The Hashtable class uses a technique referred to as rehashing.

    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 Dictionary uses a technique referred to as chaining.

    With rehashing, in the event of a collision the hash is recomputed, and the new slot corresponding to a hash is tried. With chaining, however, a secondary data structure is utilized to hold any collisions. Specifically, each slot in the Dictionary has an array of elements that map to that bucket. In the event of a collision, the colliding element is prepended to the bucket's list.

提交回复
热议问题