Home  >  Article  >  Backend Development  >  Why are dictionaries preferred over hash tables in C#?

Why are dictionaries preferred over hash tables in C#?

王林
王林forward
2023-08-28 16:29:02565browse

为什么在 C# 中字典比哈希表更受青睐?

The Hashtable class represents a collection of key-value pairs organized according to the hash code of the key. It uses keys to access elements in the collection.

A dictionary is a collection of keys and values ​​in C#. Dictionary is contained in the System.Collection.Generics namespace.

Hashtable is slower than Dictionary. For strongly typed collections, Dictionary collections are faster.

Suppose we need to find a key from a Hashtable collection. This way, we can also find a key from the Dictionary collection. In this case, for the same statement, dictionary will be faster -

for HashTable

hashtable.ContainsKey("12345");

for Dictionary

dictionary.ContainsKey("12345")

The above is the detailed content of Why are dictionaries preferred over hash tables in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:C# FactorialNext article:C# Factorial