Why is Dictionary preferred over Hashtable in C#?

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

    FYI: In .NET, Hashtable is thread safe for use by multiple reader threads and a single writing thread, while in Dictionary public static members are thread safe, but any instance members are not guaranteed to be thread safe.

    We had to change all our Dictionaries back to Hashtable because of this.

    0 讨论(0)
  • 2020-11-22 06:22

    Dictionary:

    • It returns/throws Exception if we try to find a key which does not exist.

    • It is faster than a Hashtable because there is no boxing and unboxing.

    • Only public static members are thread safe.

    • Dictionary is a generic type which means we can use it with any data type (When creating, must specify the data types for both keys and values).

      Example: Dictionary<string, string> <NameOfDictionaryVar> = new Dictionary<string, string>();

    • Dictionay is a type-safe implementation of Hashtable, Keys and Values are strongly typed.

    Hashtable:

    • It returns null if we try to find a key which does not exist.

    • It is slower than dictionary because it requires boxing and unboxing.

    • All the members in a Hashtable are thread safe,

    • Hashtable is not a generic type,

    • Hashtable is loosely-typed data structure, we can add keys and values of any type.

    0 讨论(0)
  • 2020-11-22 06:23

    HashTable:

    Key/value will be converted into an object (boxing) type while storing into the heap.

    Key/value needs to be converted into the desired type while reading from the heap.

    These operations are very costly. We need to avoid boxing/unboxing as much as possible.

    Dictionary : Generic variant of HashTable.

    No boxing/unboxing. No conversions required.

    0 讨论(0)
  • 2020-11-22 06:24

    In most programming languages, dictionaries are preferred over hashtables

    I don't think this is necessarily true, most languages have one or the other, depending on the terminology they prefer.

    In C#, however, the clear reason (for me) is that C# HashTables and other members of the System.Collections namespace are largely obsolete. They were present in c# V1.1. They have been replaced from C# 2.0 by the Generic classes in the System.Collections.Generic namespace.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 06:27

    The Hashtable is a loosely-typed data structure, so you can add keys and values of any type to the Hashtable. The Dictionary class is a type-safe Hashtable implementation, and the keys and values are strongly typed. When creating a Dictionary instance, you must specify the data types for both the key and value.

    0 讨论(0)
提交回复
热议问题