Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between KeyValuePair and Hashtable in .NET?

Tags:

c#

.net

I want to store the my custom collection as a Key and Value is also a collection of string List.I may achieve this using both KeyvaluePair and hashtable.What is the best suitable collction which gives me more advantage in terms of flexibility?

like image 336
Ashish Ashu Avatar asked Nov 23 '09 05:11

Ashish Ashu


People also ask

What is the difference between HashTable and dictionary in C# net?

In Hashtable, you can store key/value pairs of the same type or of the different type. In Dictionary, you can store key/value pairs of same type. In Hashtable, there is no need to specify the type of the key and value. In Dictionary, you must specify the type of key and value.

What is the difference between HashMap and HashTable in C#?

HashMap is non-syncronized and is not thread safe while HashTable is thread safe and is synchronized. HashMap allows one null key and values can be null whereas HashTable doesn't allow null key or value. HashMap is faster than HashTable. HashMap iterator is fail-safe where HashTable iterator is not fail-safe.

What is faster in C#: dictionary or HashTable?

Dictionary is faster than hashtable as dictionary is a generic strong type. Hashtable is slower as it takes object as data type which leads to boxing and unboxing.

What is diff between HashTable and dictionary?

Hashtable and Dictionary are collection of data structures to hold data as key-value pairs. Dictionary is generic type, hash table is not a generic type. The Hashtable is a weakly typed data structure, so you can add keys and values of any Object Type to the Hashtable.


1 Answers

Hashtable is random access and internally uses System.Collections.DictionaryEntry for its items from .NET 1.1; whereas a strongly typed System.Collections.Generic.Dictionary in .NET 2.0 uses System.Collections.Generic.KeyValuePair items and is also random access.

(Note: This answer is biased toward the .NET 2.0 framework when providing examples - that's why it continues with KeyValuePair instead of DictionaryEntry - the original question indicates this is the desired Type to work with.)

Because KeyValuePair is an independent class, you can manually make a List or Array of KeyValuePair instances, but a list or array will be sequentially accessed. This is in contrast to the Hashtable or Dictionary which internally creates its own element instances and is randomly accessed. Both are valid ways of using KeyValuePair instances. Also see see MSDN info about selecting a Collection class to use.

In summary: sequential access is fastest when using a small set of items whereas a larger set of items benefits from random access.

Microsoft's hybrid solution: An interesting specialized collection introduced in .NET 1.1 is System.Collections.Specialized.HybridDictionary which uses a ListDictionary internal representation (sequentially accessed) while the collection is small, and then automatically switches to a Hashtable internal representation (randomly accessed) when the collection gets large".

C# Sample Code

The following samples show the same Key-Value pair instances created for different scenarios - sequential access (two examples) followed by one example of random access. For simplicity in these examples they will all use an int key with string value - you can substitute in the data types you need to use.

Here's a strongly-typed System.Collections.Generic.List of key-value pairs.
(Sequential access)

// --- Make a list of 3 Key-Value pairs (sequentially accessed) ---
// build it...
List<KeyValuePair<int, string>> listKVP = new List<KeyValuePair<int, string>>();
listKVP.Add(new KeyValuePair<int, string>(1, "one"));
listKVP.Add(new KeyValuePair<int, string>(2, "two"));
// access first element - by position...
Console.Write( "key:" + listKVP[0].Key + "value:" + listKVP[0].Value );

Here's a System.Array of key-value pairs.
(Sequential access)

// --- Make an array of 3 Key-Value pairs (sequentially accessed) ---
// build it...
KeyValuePair<int, string>[] arrKVP = new KeyValuePair<int, string>[3];
arrKVP[0] = new KeyValuePair<int, string>(1, "one");
arrKVP[1] = new KeyValuePair<int, string>(2, "two");
// access first element - by position...
Console.Write("key:" + arrKVP[0].Key + "value:" + arrKVP[0].Value);

Here's a Dictionary of Key-Value pairs.
(Random access)

// --- Make a Dictionary (strongly typed) of 3 Key-Value pairs (randomly accessed) ---
// build it ...
Dictionary<int, string> dict = new Dictionary<int, string>();
dict[1] = "one";
dict[2] = "two";
// access first element - by key...
Console.Write("key:1 value:" + dict[1]); // returns a string for key 1
like image 110
14 revs Avatar answered Nov 15 '22 06:11

14 revs