为什么在C#中字典比哈希表更受青睐? [英] Why is Dictionary preferred over Hashtable in C#?

查看:338
本文介绍了为什么在C#中字典比哈希表更受青睐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数编程语言中,字典比哈希表更受青睐。
背后的原因是什么?

In most programming languages, dictionaries are preferred over hashtables. What are the reasons behind that?

推荐答案

就其价值而言,词典 >(在概念上)哈希表。

For what it's worth, a Dictionary is (conceptually) a hash table.

如果您的意思是为什么我们要使用 Dictionary< TKey,TValue> 类而不是 Hashtable 类?,那么答案很简单: Dictionary< TKey,TValue> 是一个泛型类型,不是 Hashtable 。这意味着您可以使用 Dictionary< TKey,TValue> 获得类型安全性,因为您不能在其中插入任何随机对象,也不必强制转换值

If you meant "why do we use the Dictionary<TKey, TValue> class instead of the Hashtable class?", then it's an easy answer: Dictionary<TKey, TValue> is a generic type, Hashtable is not. That means you get type safety with Dictionary<TKey, TValue>, because you can't insert any random object into it, and you don't have to cast the values you take out.

有趣的是,.NET Framework中的 Dictionary< TKey,TValue> 实现基于 Hashtable ,您可以从源代码中的此注释中看出:

Interestingly, the Dictionary<TKey, TValue> implementation in the .NET Framework is based on the Hashtable, as you can tell from this comment in its source code:


通用词典是从Hashtable的源中复制的

The generic Dictionary was copied from Hashtable's source

来源

这篇关于为什么在C#中字典比哈希表更受青睐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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