Differences between hashtable and Dictionary

Source: Internet
Author: User
Dark Horse Programmer -- Yan Zhendong ---------------------- ASP. NET + unity3d game development,. Net training, hope to communicate with you! -------------------- First, both hashtable and dictionary belong to the dictionary class, which is the set of key-value pairs. Similarities: we can add key-value pairs to them using the add () function. And addrange () functions. You can also remove move moverange moveat and use the key to find the value. Differences: For value types, the performance of a dictionary of a specific type (excluding objects) is better than that of hashtable, because the hashtable element belongs to the object type, therefore, the packing and unboxing operations are often triggered when the value type is stored or retrieved.

The dictionary class and the hashtable class have the same functions.

Hashtable ht = new hashtable (); // implement the idictionary Interface
Ht. Add (1, "");
Ht. Add (2, "B ");
Ht. Add (3, "C ");
Foreach (dictionaryentry de in HT) // hashtable returns the dictionaryentry type
{
De. Key;
De. value;
}

Dictionary <int, string> mydictionary = new dictionary <int, string> (); // implements the idictionary interface and the idictionary <t key, T value> class.
Mydictionary. Add (1, "");
Mydictionary. Add (2, "B ");
Mydictionary. Add (3, "C ");
Foreach (int I in mydictionary. Keys)
{
Console. writeline ("Key =" + I + "value =" + mydictionary );
}
Or
Foreach (keyvaluepair <string, double> temp in mydictionary) // The returned keyvaluepair <string, double> Generic Array
{
Temp. Key;
Temp. value;
}

Both get values through keys through loops.

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.