字典和哈希表的区别 [英] Difference between Dictionary and Hashtable

查看:20
本文介绍了字典和哈希表的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
为什么在 C# 中字典比哈希表更受欢迎?

Dictionary 和 Hashtable 有什么区别.如何决定使用哪一个?

What is the difference between Dictionary and Hashtable. How to decide which one to use?

推荐答案

简单来说,Dictionary 是一个泛型类型,允许:

Simply, Dictionary<TKey,TValue> is a generic type, allowing:

  • 静态类型(和编译时验证)
  • 无需拳击即可使用

如果您是 .NET 2.0 或更高版本,您应该首选 Dictionary(和其他通用集合)

If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections)

一个微妙但重要的区别是 Hashtable 支持多个读取器线程和一个写入器线程,而 Dictionary 不提供线程安全.如果您需要通用字典的线程安全,则必须实现自己的同步或(在 .NET 4.0 中)使用 ConcurrentDictionary.

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>.

这篇关于字典和哈希表的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆