Home  >  Article  >  Backend Development  >  Hash tables and dictionaries in C#

Hash tables and dictionaries in C#

WBOY
WBOYforward
2023-09-10 23:29:08657browse

C# 中的哈希表与字典

Hash table

A hash table is used when a key is needed to access an element, and useful key values ​​can be identified. Each item in the hash table has a key/value pair. Keys are used to access items in the collection.

Members in the hash table are thread-safe. If we try to find a key that doesn't exist, it will return null. Hashtable is not a generic type.

Hashtable collection is slower than dictionary because it requires boxing and unboxing.

Declaration Hashtable -

Hashtable ht = new Hashtable();

Dictionary

A dictionary is a collection of keys and values ​​in C#. Dictionary is contained in the System.Collection.Generics namespace. Dictionary is a generic type and will return an error if you try to find a key that doesn't exist.

Dictionary collections are faster than Hashtables because there is no boxing and unboxing.

Declare a dictionary -

IDictionary<int, string> d = new Dictionary<int, string>();

The above is the detailed content of Hash tables and dictionaries 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