您的位置:首页 > 产品设计 > UI/UE

KeyValuePair<TKey, TValue>是DictionaryEntry的泛型版本

2013-03-04 14:31 495 查看
如同Dictionary<TKey, TValue>是HashTable的泛型版本一样,KeyValuePair<TKey, TValue>是DictionaryEntry的泛型版本。

Dictionary与HashTable内部是基于hash算法的,而SortedDictionary和SortedList是基于Red Black Tree(红黑树)的。

DictionaryEntry:

Hashtable openWith = new Hashtable();

// Add some elements to the hash table. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// When you use foreach to enumerate hash table elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach (DictionaryEntry de in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}


KeyValuePair<TKey, TValue>:

Dictionary<string, string> openWith =
new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
foreach( KeyValuePair<string, string> kvp in myDictionary )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}


OO与面向接口:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: