对于它的价值,一个字典(概念)的哈希表。
如果你的意思是 “为什么我们使用Dictionary<TKey, TValue>类而不是Hashtable类?”,那么这是一个简单的答案: Dictionary<TKey, TValue>是泛型类型, Hashtable不是。这意味着您可以使用Dictionary<TKey, TValue>获得类型安全性,因为您无法在其中插入任何随机对象,并且您不必转换所取出的值。
有趣的是,.NET Framework 中的Dictionary<TKey, TValue>实现基于Hashtable ,您可以从源代码中的注释中看出:
通用词典是从 Hashtable 的源代码复制而来的
Dictionary <<<>>> Hashtable差异:
Synchronized()方法提供线程安全版本KeyValuePair <<<>>> 枚举项: DictionaryEntry Dictionary / Hashtable相似:
GetHashCode()方法类似的 .NET 集合(候选使用而不是 Dictionary 和 Hashtable):
ConcurrentDictionary - 线程安全 (可以同时从多个线程安全地访问) HybridDictionary - 优化的性能 (适用于少数项目以及许多项目) OrderedDictionary - 可以通过 int index 访问值(按添加项目的顺序) SortedDictionary - 项目自动排序 StringDictionary - 强类型并针对字符串进行优化 因为Dictionary是一个泛型类( Dictionary<TKey, TValue> ),所以访问它的内容是类型安全的(即你不需要像使用Hashtable一样从Object )。
相比
var customers = new Dictionary<string, Customer>();
...
Customer customer = customers["Ali G"];至
var customers = new Hashtable();
...
Customer customer = customers["Ali G"] as Customer;但是, Dictionary在内部实现为哈希表,因此从技术上讲它的工作方式相同。