The difference between C # hashtable and dictionary

Source: Internet
Author: User

The difference between Hashtable and dictionary

  1.HashTable

A hash table (HashTable) represents a collection of key/value pairs. In the. NET Framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like Key-value key-value pairs, where key is usually used for quick lookups, and key is case-sensitive; value is used to store the value corresponding to key. Key-value Key-value pairs in Hashtable are all object types, so hashtable can support any type of KeyValue key-value pair, and any non-null object can be used as a key or a value.

Add a key/key value pair to the hash table: Hashtableobject.add (key,);

Remove a key/key value pair in the hash table: Hashtableobject.remove (key);

Remove all elements from the hash table: Hashtableobject.clear ();

Determines whether a hash table contains a specific key key:HashtableObject.Contains (key);

2. Dictionary

Dictionary represents a collection of keys and values.

Dictionary<string, string> is a generic

He has a set of functions that can sometimes be seen as an array.

His structure is this: Dictionary<[key], [value]>

His feature is that the deposit object is required to be stored with the [key] value one by one in the generic

To find the corresponding value by a certain [key]

3. The difference between Hashtable and dictionary:

(1). Hashtable does not support generics, while dictionary supports generics.

(2). The Hashtable element is of type Object, so boxing and unpacking is usually the case when storing or retrieving a value type, so you may need to do some type conversion, and it can be time-consuming to int,float these value types for boxing.

(3). Dictionary is recommended in single-threaded programs, with a generic advantage, with faster read speeds and fuller capacity utilization. Hashtable is recommended in multithreaded programs, the default Hashtable allows single-threaded writes, multithreaded reads, and further calls to the Hashtable Synchronized () method to obtain a fully thread-safe type. While Dictionary is non-thread-safe, it must be protected by the use of the lock statement, and the efficiency is greatly reduced.

(4) When passing the code test found that key is an integer type dictionary efficiency than hashtable faster, if key is a string type, dictionary efficiency is not hashtable fast.

static void Intmethod () {int count = 1000000;            Dictionary<int, int> Dictionary = new Dictionary<int, int> ();            Hashtable Hashtable = new Hashtable (); for (int i = 0; i < count; i++) {dictionary.                ADD (I,i); Hashtable.            ADD (I,i);            } Stopwatch Stopwatch = Stopwatch.startnew ();            for (int i = 0; i < count; i++) {int value = Dictionary[i]; } stopwatch.            Stop (); Console.WriteLine (stopwatch.            Elapsedmilliseconds);            Stopwatch = Stopwatch.startnew ();            for (int i = 0; i < count; i++) {Object value = Hashtable[i]; } stopwatch.            Stop (); Console.WriteLine (stopwatch.         Elapsedmilliseconds);            } static void Methodstring () {int count = 1000000; dictionary<string, string> Dictionary = new DIctionary<string, string> ();            Hashtable hashtable=new Hashtable (); for (int i = 0; i < count; i++) {dictionary.                ADD (i.ToString (), "AAA"); Hashtable.            ADD (i.ToString (), "AAA");            } Stopwatch Stopwatch = Stopwatch.startnew ();            for (int i = 0; i < count; i++) {string value=dictionary[i.tostring ()]; } stopwatch.            Stop (); Console.WriteLine (stopwatch.            Elapsedmilliseconds);            Stopwatch = Stopwatch.startnew ();            for (int i = 0; i < count; i++) {Object value = hashtable[i.tostring ()]; } stopwatch.            Stop (); Console.WriteLine (stopwatch.        Elapsedmilliseconds); }

The difference between C # hashtable and dictionary

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.