简体   繁体   中英

What are the differences b/w Hashtable, Dictionary and KeyValuePair?

I use Dictionary in my code but my colleagues use Hashtable. MSDN says they work on Key Value pair & examples of Hashtable and dictionary are same on MSDN.

Then how different are they from each other & which is the best of them or are they suited for difference occasions?

Hashtable is an untyped associative container that uses DictionaryEntry class to return results of enumeration through its key-value pairs.

Dictionary<K,T> is a generic replacement of Hashtable that was introduced in C# 2.0. It uses KeyValuePair<K,T> generic objects to represent its key-value pairs.

The only place where you should see Hashtable these days is legacy code that must run on .NET 1.1, before generics have been introduced. It's been kept around for compatibility reasons, but you should prefer Dictionary<K,T> whenever you can.

KeyValuePair is the unit of data stored in a Hashtable (or Dictionary ). They are not equivalent to each other.

A key value pair contains a single key and a single value. A dictionary or hashtable contains a mapping of many keys to their associated values.

KeyValuePair is useful when you want to store two related pieces of information as a single unit, especially when one is related to the other in an identifying way (for instance 1234 => "David Smith"). They are also what you get back when you iterate a dictionary. In .NET 4.0, these are really only meant for use internally in a Dictionary- the Tuple class has been introduced for general purpose use.

The difference between Hashtable and Dictionary is that Hashtable is not a generic class- both it's keys and values are of type Object . Dictionary is generic, and should generally be used in favor of Hashtable in any new development.

A dictionary is a typed hashtable. If you know the data type of the key and value, use a dictionary for performance reasons (avoid casting).

One major difference is that Hashtable is thread-safe, while Dictionary is not.

The documentation says:

Hashtable is thread safe for use by multiple reader threads and a single writing thread. It is thread safe for multi-thread use when only one of the threads perform write (update) operations, which allows for lock-free reads provided that the writers are serialized to the Hashtable . To support multiple writers all operations on the Hashtable must be done through the wrapper returned by the Synchronized method, provided that there are no threads reading the Hashtable object.

Contrast this with the Dictionary documentation :

A Dictionary(Of TKey, TValue) can support multiple readers concurrently, as long as the collection is not modified.

KeyValuePair<TKey, TValue> is a type used by Dictionary<TKey, TValue> . When you iterate over the items in a Dictionary, you get a series of KeyValuePair objects.

Here's some sample usage:

var dict = new Dictionary<string, int>();

dict.Add("Hello", 1);

foreach (KeyValuePair<string, int> entry in dict)
{
    string s = entry.Key;
    int i = entry.Value;

    // More logic here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM