博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dictionary、KeyValuePair、Hashtable的比较和使用
阅读量:7061 次
发布时间:2019-06-28

本文共 3709 字,大约阅读时间需要 12 分钟。

 

Dictionary

官方文档:http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

 

而Directory<T,T>是HashTable的泛型模式,用KeyValuePair<T,T>来遍历Directory<T,T>。

 

Dictionary泛型 中的任何键都必须是唯一的。键不能为空引用,但是如果值类型 TValue 为引用类型,该值则可以为空。

Dictionary 调用 Add 方法之前使用 ContainsKey 方法测试某个键是否存在,否则得到一个KeyNotFoundException。

if (!openWith.ContainsKey("ht")){    openWith.Add("ht", "hypertrm.exe");    Console.WriteLine("Value added for key = \"ht\": {0}",  openWith["ht"]);}

当程序频繁尝试字典中不存在的键时,使用 TryGetValue 方法来检索值,这种方法是一种更有效的检索值的方法。

string value = "";if (openWith.TryGetValue("tif", out value)){    Console.WriteLine("For key = \"tif\", value = {0}.", value);}

测试键是否存在的最有效的方法

// The indexer throws an exception if the requested key is not in the dictionary.try{    Console.WriteLine("For key = \"tif\", value = {0}.",    openWith["tif"]);}catch (KeyNotFoundException){    Console.WriteLine("Key = \"tif\" is not found.");}

相关文档:https://msdn.microsoft.com/zh-cn/library/kw5aaea4(v=vs.110).aspx

 

字典集合的两种遍历

Dictionary
dictionary = new Dictionary
();foreach (string dic in dictionary.Keys){ //Key: dic //Value: dictionary[dic]}foreach (KeyValuePair
dic in dictionary{ //Key: dic.Key //Value: dic.Value }

KeyValuePair

结构体,定义可设置或检索的键/值对。

性能比自定义结构体查,可阅读:http://www.cnblogs.com/Artemisblog/p/3706054.html

示例

/// /// 设置键/值对/// /// 
private KeyValuePair
SetKeyValuePair(){ int intKey = 1; string strValue = "My value"; KeyValuePair
kvp = new KeyValuePair
(intKey, strValue); return kvp;}///
/// 获得键/值对/// private void GetKeyValuePairDemo(){ KeyValuePair
kvp = SetKeyValuePair(); int intKey = kvp.Key; string strValue = kvp.Value;}

 

http://www.cnblogs.com/Artemisblog/p/3706054.html

http://www.cnblogs.com/KTblog/p/4392248.html

 

 

HashTable

描述

用于处理和表现类似keyvalue的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中keyvalue键值对均为object类型,所以Hashtable可以支持任何类型的keyvalue键值对.

 Hashtable每个元素都是一个存储在 DictionaryEntry 对象中的键/值对。键不能为 空引用,但值可以。

二,哈希表的简单操作

Hashtable hshTable = new Hashtable(); //  创建哈希表hshTable .Add("Person1",  "zhanghf");  //  往哈希表里添加键值对hshTable .Clear();  //移除哈希表里所有的键值对hshTable .Contains("Person1");   //判断哈希表里是否包含该键string name = (string)hshTable["Person1"].ToString(); //取哈希表里指定键的值hshTable.Remove("Person1"); //  删除哈希表里指定键的键值对IDictionaryEnumerator en = hshTable.GetEnumerator();  //  遍历哈希表所有的键,读出相应的值while (en.MoveNext()){     string str = en.Value.ToString(); }// 循环hashtableforeach (DictionaryEntry de in hshtable) {...}

 

可以使用Dictionary实现的时候,不要用哈希表。使用值类型时,它需要进行封箱和拆箱,耗费内存资源,效率比较低。

 

http://www.cnblogs.com/liuwenjun830/archive/2006/07/28/462182.html【推荐】

http://www.cnblogs.com/yangleiWPF/archive/2010/11/16/1878494.html

http://www.cnblogs.com/gjcn/archive/2008/07/07/1234560.html

 

 

 参考文章:

C#中Dictionary的用法【推荐】

http://jingyan.baidu.com/article/9989c7460ab872f648ecfeed.html

 

C#中键值对类型Hashtable与Dictionary比较和相关用法

http://www.2cto.com/kf/201304/201715.html

Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是不同的数据结构。就像Java中的ArrayList与LinkList,虽然都是实现了List接口作为集合载体,但其内部结构是不一样的, ArrayList是通过数组实现的,LinkList是通过对象链表实现的。由于 Hashtable 和 Dictionary 同时存在, 在使用场景上必然存在选择性, 并不任何时刻都能相互替代.[1] 单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.[2] 多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.[3] Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.个人偏向于使用Dictionary,因为其使用泛型,可控性比较强。在一些资料中,很多人也推荐使用Dictionary,其原因主要有1、Dic是类型安全的,这有助于我们写出更健壮更具可读性的代码,而且省却我们强制转化的麻烦。2、Dic是泛行的,当K或V是值类型时,其速度远远超过Hashtable。处理对象时不需要进行显式或是隐式转型,进行值类型处理时不需要装箱。所以使用方便一些, 而且效率会高一些(编码效率、运行效率),还不太容易出错。

 

 

 

 

 

你可能感兴趣的文章