On the code examples of Hashtable and dictionary in C #

Source: Internet
Author: User
on the code examples of C # Hashtable and dictionary:

Dictionary<tkey, Tvalue> () Hashtable ()

First, the stored data type

Hashtable is not generic, is not type-safe, dictionary is generic, and is type-safe;

The key values of the Hashtable are of type object, but the data type of the dictionary's key value can be specified.

That is, if a data type other than object is stored in the Hashtable, the type conversion required to display it when the data is fetched is used normally. and dictionary does not have this problem.

In this respect, Hashtable is the equivalent of dictionary<object,object>.

            Hashtable ht = new Hashtable ();            dictionary<string, int> dic = new dictionary<string, int> ();            Ht. ADD ("A", 1);            Dic. ADD ("A", 1);            Console.WriteLine (ht["A"]+1);     Compile Error! The object type cannot be added directly to the INT type.              Console.WriteLine ((int) ht["A"] + 1);//compile through, output is: 2            Console.WriteLine (dic["A"] + 1);    Compiled, output is: 2

Second, the order in which the data is read is consistent with the order in which it is added

The order in which data is read by dictionary and Hashtable is not guaranteed, or can be said to be inconsistent, in the order of data being added.

Dictionary the order in which the data is read and the order in which it is added is consistent when it is added without deleting it;

However, after deleting and adding operations, it is not guaranteed that the order in which the data is read is consistent with the order in which it was added.

Dictionary<int, int> dic = new Dictionary<int, int> ();            Dic. ADD (0, 0);            Dic. ADD (1, 1);            Dic. ADD (2, 2);            Console.WriteLine ("Only after adding element processing:");            foreach (Keyvaluepair<int, int> kvp in dic)            {                Console.WriteLine ("Key:" + kvp. Key + "Value:" + kvp. Value);            }            Dic. Remove (0);            Dic. ADD (3, 3);            Console.WriteLine ("After deleting and adding element processing:");            foreach (Keyvaluepair<int, int> kvp in dic)            {                Console.WriteLine ("Key:" + kvp. Key + "Value:" + kvp. Value);            }            Console.readkey ();

For dicitionary, if an element is removed from it, then the newly added element will fill the position of the deleted element, thus causing the order of the added data to be inconsistent with the order in which the data is read.

For Hashtable, its data storage order is calculated according to a certain algorithm, so in most cases, its data reading order and data addition order is inconsistent.

So if you need to keep the order of data added, it's best not to use dictionary and Hashtable.

Third, when using a non-existent key value to Hashtable or dictionary value

For Hashtable, a null is returned if the value is taken with a nonexistent key value;

            Hashtable ht = new Hashtable ();            Console.WriteLine (ht["B"]==null);            Console.readkey ();

For dictionary, an exception of type "System.Collections.Generic.KeyNotFoundException" is thrown if the value is taken with a nonexistent key value.

Therefore, when you take a value from dictionary or Hashtable, you can first determine whether the key value exists (judged by the ContainsKey () method) to prevent a value or exception from being expected.

IV. Thread SAFETY

Dictionary is not thread-safe, and Hashtable is thread-safe.

Related Article

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.