字典和Hashtable的区别 [英] Difference between Dictionary and Hashtable
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/301371/why-dictionary-is-$p$pferred-over-hashtable-in-c\">Why字典是preferred在C#中的哈希表?
什么是字典和Hashtable的区别。如何决定使用哪一个呢?
What is the difference between Dictionary and Hashtable. How to decide which one to use?
推荐答案
简单地说,词典&LT; TKEY的,TValue&GT;
是一个泛型类型,允许:
Simply, Dictionary<TKey,TValue>
is a generic type, allowing:
- 静态类型(和编译时验证)
- 没有拳击使用
如果你是.NET 2.0或以上,你应该的 preFER 的词典&LT; TKEY的,TValue&GT;
(以及其他泛型集合)
If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue>
(and the other generic collections)
有一个微妙但重要的区别是,的Hashtable
支持与单个作家线程多读线程,而词典
报价没有线程安全。如果你需要线程安全与通用字典,你必须实现自己的同步或(在.NET 4.0中)使用 ConcurrentDictionary&LT; TKEY的,TValue方式&gt;
A subtle but important difference is that Hashtable
supports multiple reader threads with a single writer thread, while Dictionary
offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) use ConcurrentDictionary<TKey, TValue>
.
这篇关于字典和Hashtable的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!