开发者

What is the difference between KeyValuePair and Hashtable in .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?


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


One relevant bit is that Hashtable is a .Net 1.1 class, whereas KeyValuePair was introduced in .NET 2.0. (with the introduction of generics)


Hashtable was created when C# didn't support generics yet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜