差异黑白哈希表和字典 [英] diff. b/w hashtable and dictionary
本文介绍了差异黑白哈希表和字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道到底是什么差异.哈希表和带有示例的字典.
i want to know what is exactly diff. hashtable and dictionary with example.
thanks in advance.
推荐答案
非常粗略:类System.Collections.Hashtable
是过时的版本,其功能与通用类System.Collections.Generic.Dictionary
非常相似在.NET Framework v.2.0中实现之前创建的通用类.
该类没有用obsolete
属性正式标记,因为它没有任何问题.可以在旧代码中使用它,但是在新代码中使用它是完全没有意义的.泛型更好,因为您不需要使用容易出错的类型转换.而是从这三个类System.Collections.Generic.Dictionary
,System.Collections.Generic.SortedDictionary
或System.Collections.Generic.SortedList
, http://msdn中选择.microsoft.com/en-us/library/0sbxh9x2.aspx [ ^ ].
它们的实现方式不同,但是从使用代码的角度来看,它们的主要区别在于性能和冗余之间的不同权衡.
—SA
Very roughly: the classSystem.Collections.Hashtable
is obsolete version with functionality very similar to the generic classSystem.Collections.Generic.Dictionary
created before generic were implemented in v.2.0 of the .NET Framework.
This class was not formally marked by theobsolete
attribute, because there is nothing wrong with it; and it can be used in legacy code, but using it in new code is totally pointless. Generics are way better, because you don''t need to use error-prone typecasts. Instead, choose from these three classesSystem.Collections.Generic.Dictionary
,System.Collections.Generic.SortedDictionary
orSystem.Collections.Generic.SortedList
, http://msdn.microsoft.com/en-us/library/0sbxh9x2.aspx[^].
They are different in implementations, but, from the standpoint of the using code, they are different mainly in the different trade-off between performance and redundancy.
—SA
下面的链接为您提供了您想要的所有区别,但是,我会在发布之前尝试找到自己的答案:
http://msdn.microsoft.com/en-us/library/4yh14awz.aspx [ ^ ]
Below link provides you all the differences you want, however, i would try to find answers my self before posting:
http://msdn.microsoft.com/en-us/library/4yh14awz.aspx[^]
ADictionary
是Hashtable
的通用版本. 在Hashtable
中,您只能将object
作为键和值添加.
在Dictionary
中,可以选择键的类型和值:
ADictionary
is a generic version of aHashtable
.
In aHashtable
, you can only addobject
s as a key and a value.
In aDictionary
, you can choose the type for the key and for the value:
Hashtable hashtable1 = new Hashtable();
hashtable1.Add(1, "HashtableValue1"); // add a key/value pair to the Hashtable
Dictionary<int,> dictionary1 = new Dictionary<int,>();
dictionary1.Add(1, "DictionaryValue1"); // add a key/value pair to the Dictionary
string hashtableValue1 = (string)hashtable1[1]; // get the value of the Hashtable and cast it to a string
string dictionaryValue1 = dictionary1[1]; // get the value of the Dictionary, you don't need to cast the value to a string
希望这会有所帮助.
Hope this helps.
这篇关于差异黑白哈希表和字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文