Use of hash tables in C # (Dictionary) __c#

Source: Internet
Author: User

Dictionary (TKey, TValue)

Represents a collection of keys and values.

Dictionary (TKey, TValue) generic classes provide mappings from a set of keys to a set of values. Each added item in the dictionary consists of a value and its associated key. The speed of retrieving values by key is very fast, close to O (1), because the Dictionary (TKey, TValue) class is implemented as a hash table. (The retrieval speed depends on the quality of the hash algorithm for the type specified for TKey.) )

As long as the object is used as a key in Dictionary (TKey, TValue), it cannot be changed in any way that affects its hash value. Any key in Dictionary (TKey, TValue) must be unique when using the dictionary's equality comparer comparison. The key cannot be null. However, if the value type TValue to a reference type, the value can be null.

Dictionary (TKey, TValue) requires an equality implementation to determine whether the keys are equal. You can use a constructor that accepts the comparer parameter to specify the implementation of the IEqualityComparer (t) generic interface, or use the default generic equality comparer Equalitycomparer (t) if you do not specify an implementation. Default. If the type TKey implements System. iequatable< (of < (t>) >) generic interfaces, the default equality comparer uses that implementation.

The capacity of Dictionary (TKey, TValue) is the number of elements that Dictionary (TKey, TValue) can contain. When elements are added to Dictionary (TKey, TValue), the capacity is automatically increased as needed by reallocating the internal array.

For enumerations, each item in the dictionary is treated as a keyvaluepair (TKey, TVALUE) structure that represents a value and its key. The order returned by the item is undefined.

The C # language's foreach statement (in C + + for each, in Visual Basic for each) requires the type of each element in the collection. Because Dictionary (TKey, TValue) is a collection of keys and values, the element type is not a key type or a value type. Instead, the element type is the KeyValuePair (TKey, TValue) of the key type and the value type. For example:

Members of this type of public static (Shared in Visual Basic) are thread-safe. However, there is no guarantee that any instance members are thread safe.

As long as you do not modify the collection, Dictionary (TKey, TValue) can support multiple readers at the same time. Even so, enumerating a collection from beginning to end is inherently not a thread-safe process. When the rare occurrence of enumeration and write-access contention occurs, the collection must be locked throughout the enumeration. To allow multiple threads to access the collection for read-write operations, you must implement your own synchronization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.