问题描述
System.Collections.Hashtable对象已经过时了吗?
Has the System.Collections.Hashtable object become obsolete?
随着C#v2和v3中泛型的实现和完善,自从我这样做已经很长时间了。我们发现哈希表比通用字典更合适。从字面上看,我不记得我上次使用Hashtable的情况。
With the implementation and refinements in generics from C# v2 and v3, it's been a long time since I've found a Hashtable more appropriate than a generic Dictionary. Literally, I can't recall the last time I used Hashtable.
只是想知道是否有人发现Hashtable仍然适合或偏爱于实现,决定的基础-易用性,性能,集合大小,对象类型等。
Just wondering if anyone else has found a case where a Hashtable is still appropriate or preferred for implementation and the basis for that decision -- ease of use, performance, collection size, object type, etc.
更新:是将这个问题限制为C#。
UPDATE: was meaning to limit this question to C#.
推荐答案
除了显而易见的情况,您还必须在期望使用现有API的现有API中使用哈希表(例如尚不支持通用的语言)类型)。如果您需要从COM访问它们,则可能还需要使用它们。我相信Hashtable是COM可见的,而Dictionary是不可见的。
Aside from the obvious case where you must use a Hashtable in an existing API that expects one (such as languages that don't yet support generic types). You also may need to use them if you need to access them from COM. I believe that Hashtable is COM visible, whereas Dictionary is not.
除此之外,您通常要使用Dictionary<>,因为它避免了装箱(性能下降)并且提供类型安全性。
Other than that, you generally want to use Dictionary<> because it avoids boxing (a performance hit) and provides type safety.
这篇关于哈希表对象仍然有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!