关于c#:Dictionary和Hashtable之间的区别

Difference between Dictionary and Hashtable

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Why Dictionary is preferred over hashtable in C#?

字典和哈希表的区别是什么?如何决定使用哪一个?


(P)简单,EDOCX1 0音标是一个普通类型,Allowing:(p)

  • Static typing(and compile-time verification)
  • Use without boxing

(P)如果你是Net 2.0或Above,你应该更喜欢用英文写的EDOCX1(和其他性别群体)。(p)(P)a Subtle but important difference is that EDOCX1 plus 2 common supports multiple reader threads with a single writer thread,while EDOCX1 common 3 offers no thread safety.如果你需要一个通用词典的三个安全性,你必须执行你自己的系统或(在NET 4.0)使用字母名称4。(p)


(P)Lets give an example that would explain the difference between HashTable and Dictionary.(p)(P)这是一种方法,工具HashTable(p)字母名称(P)The following is for dictionary(p)字母名称


(P)在Hashtable和Dictionary之间有一个更重要的区别。如果你使用索引来获得一个Hashtable之外的价值,那么,Hashtable将成功地返回无效,以一个不存在的项目,如果你尝试访问一个项目而使用一个不存在于字典中的索引,那么词典将是一个错误。(p)


(P)Dictionary is typed(so valuetypes don't need boxing),a Hashtable ISN't(so valuetypes need boxing).Hashtable has a nicer way of obtaining a value than dictionary imho,because it always knows the value is an object.Though if you're using Net 3.5,it's easy to write an extension method for dictory to get similar behavior.(p)(P)如果你需要多个关键的价值,检查我的源代码多价值词典在这里:页:1(p)


(P)Want to add a difference:(p)(P)Trying to acess a inensistent key gives runtime mistake in dictionary but no problem in Hashtable as it returns null instead of mistake.(p)(P)E.G.(p)字母名称(P)这里没有错误为关键0&;也为关键"十"(注:t is small)(p)字母名称(P)Here mistake for key 0&;also for key 10 as both are inectional in dictionary,runtime mistake,while try to acess.(p)


(P)The HashTable class is a specific type of dictionary class that used an integer value(called a hash)to assistance in the storage of its keys.The HashTable Class used the Hash to speed up the searching for a specific key in the collection.所有的目标都来自于目标阶级。This class supports the gethash method,which returns an integer that uniquely identifies the object.The HashTable Class is a very efficient collection in general.The only issue with the Hashtable class is that it requires a bit of overhead,and for small collections(fewer than ten elements)the overhead can impede performance.(p)布尔奇1


(P)Ilookup interface is used in Net 3.5 with Linq.(p)(P)The HashTable is the base class that is weakly type;the Dictionary抽象阶级是强大的类型和使用内部Hashtable。(p)(P)I found a strange thing about dictionary,when we add the multiple entries in dictionary,the order in which the entries are added is maintained.如果我就字典提出一项建议,我将在同一个顺序中取得记录。(p)(P)Whereas,this is not true with normal hasstable,as when I add same records in Hashtable the order is not maintained.As far as my knowledge goes,Dictionary is based on Hashtable,if this is true,why my Dictionary Maintains the order but HashTable does not?(p)(P)As to why they behav differently,it's because generic dictionary implements a HashTable,but is not based on system.collections.HashTable.The generic Dictionary Implementation is based on allocating key-value-pairs from a list.这些是与随机存取的HashTable Buckets不符的,但当它们返回时,它们只是随处可见的清单,它将是长期进入的命令,而不是重新使用。(p)(P)希夫·戈文Birlasoft。(p)