| Hashtable | Dictionary |
|---|---|
| A Hashtable is a non-generic collection. | A Dictionary is a generic collection. |
| Hashtable is defined under System.Collections namespace. | Dictionary is defined under System.Collections.Generic namespace. |
| In Hashtable, you can store key/value pairs of the same type or of the different type. | In Dictionary, you can store key/value pairs of same type. |
| In Hashtable, there is no need to specify the type of the key and value. | In Dictionary, you must specify the type of key and value. |
| The data retrieval is slower than Dictionary due to boxing/ unboxing. | The data retrieval is faster than Hashtable due to no boxing/ unboxing. |
| In Hashtable, if you try to access a key that doesn’t present in the given Hashtable, then it will give null values. | In Dictionary, if you try to access a key that doesn’t present in the given Dictionary, then it will give error. |
| It is thread safe. | It is also thread safe but only for public static members. |
| It doesn’t maintain the order of stored values. | It always maintain the order of stored values. |
C# 中 字典(Dictionary)和哈希表(Hashtable)的区别
最新推荐文章于 2024-07-08 11:47:57 发布
本文详细对比了Hashtable与Dictionary两种集合类型。主要从定义、类型安全性、数据存取速度及线程安全等方面进行了深入分析。
674

被折叠的 条评论
为什么被折叠?



