Why is Dictionary preferred over Hashtable in C#?

后端 未结 19 1265
长情又很酷
长情又很酷 2020-11-22 05:53

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

19条回答
  •  臣服心动
    2020-11-22 06:29

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

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

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

    The generic Dictionary was copied from Hashtable's source

    Source

提交回复
热议问题