Why is Dictionary preferred over Hashtable in C#?

后端 未结 19 1532
长情又很酷
长情又很酷 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:26

    A Hashtable object consists of buckets that contain the elements of the collection. A bucket is a virtual subgroup of elements within the Hashtable, which makes searching and retrieving easier and faster than in most collections.

    The Dictionary class has the same functionality as the Hashtable class. A Dictionary of a specific type (other than Object) has better performance than a Hashtable for value types because the elements of Hashtable are of type Object and, therefore, boxing and unboxing typically occur if storing or retrieving a value type.

    For further reading: Hashtable and Dictionary Collection Types

提交回复
热议问题