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.