简体   繁体   中英

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

我想将我的自定义集合存储为Key,Value也是字符串List的集合。我可以使用KeyvaluePair和hashtable来实现这一点。什么是最合适的匹配,这使我在灵活性方面更具优势?

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)

当C#不支持泛型时创建了Hashtable。

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