Differences between hashtable and dictionary in C #

Source: Internet
Author: User

Link: http://blog.sina.com.cn/s/blog_610b83d50100u74p.html

1: dictionary is recommended in a single-threaded program. It has the wildcard advantage, fast reading speed, and sufficient capacity utilization.
 2: hashtable is recommended in multi-threaded programs. The default hashtable allows single-threaded writing and multi-threaded reading. For hashtable, the synchronized () method can be further called to obtain a completely thread-safe type. dictionary is NOT thread-safe and must be manually protected using the lock statement, greatly reducing the efficiency.
 3: dictionary has the ability to sort data by insertion Order (Note: however, when the remove () function is called to delete a node, the order is disrupted ), therefore, it is convenient to use a dictionary to reflect the order.

For value types, the performance of the Dictionary of a specific type (excluding objects) <(of <(tkey, tvalue>)> 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.

// An exception occurs if the key does not exist when you use the indexer.
   Try
   {
       Console. writeline ("nonexistent key" "fff" "has the following key values:" + mydic ["fff"]);
   }
   Catch (keynotfoundexception ex)
   {
       Console. writeline ("the key is not found and an exception is thrown:" + ex. Message );
   }
   // The method to solve the above exception is to use contarnskey () to determine if a key exists. If you need to calculate the key value frequently, it is best to use trygetvalue to obtain the corresponding key value in the set.
   String value = "";
   If (mydictionary. trygetvalue ("fff", out value ))
   {
       Console. writeline ("nonexistent key" "fff" "has the following key values:" + value );
   }
   Else
   {   
       Console. writeline ("the corresponding key value is not found ");
   }
   
   // Use foreach below to Traverse Key-value pairs
   // The generic struct is used to store the key-value pair.
   Foreach (keyvaluepair <string, string>Temp in mydictionary)
   {
       Console. writeline (temp. Key, temp. value );
   }
   // Obtain a set of values
   Foreach (string s in mydictionary. values)
   {
       Console. writeline ("value" + S );
   }
   // Another method is worth obtaining
   Dictionary <string, string>. valuecollection values = mydictionary. values;
   Foreach (string s in values)
   {
       Console. writeline ("value:" + S );
   }
Common attributes and methods are as follows:Common attributes
 Attribute description
 Comparer
 Obtain the iequalitycomparer used to determine whether the keys in the dictionary are equal.
 Count
 Obtains the number of key/value pairs contained in a dictionary.
 Item
 Gets or sets the value associated with the specified key.
 Keys
 Obtains a set of keys in a dictionary.
 Values
 Obtains a set of values in a dictionary.
 
 Common Methods 
 Add  Mydictionary. Add (string key, double value );
 Adds the specified key and value to the dictionary.
 
 Clear  Mydictionary. Clear ();
 Remove all keys and values from dictionary.
 
Containskey   Mydictionary. containskey (string key );
 Determines whether a dictionary contains the specified key.
 Containsvalue  Mydictionary. containsvalue (double value)
 Determines whether a dictionary contains a specific value.
 
 Equals
 Overloaded. Determine whether the two object instances are equal. (Inherit from object .)
 
 GetenumeratorMydictionary. getenumerator (new dictionary <string, double> ("AAA", 11.11 );
 Returns the number of enumerations that access the dictionary cyclically.
 
 Gethashcode
 Used as a hash function of a specific type. Gethashcode is applicable to hash algorithms and data structures (such as hash tables. (Inherit from object .)
 
 Getobjectdata
 Implement the system. runtime. serialization. iserializable interface and return the data required to serialize the dictionary instance.
 
 GetType
 Obtain the type of the current instance. (Inherit from object .)
 
 Remove
 Removes the value of the specified key from dictionary.
 
 Tostring
 Returns the string of the current object. (Inherit from object .)
 
 Trygetvalue  Mydictionary. trygetvalue (string key, out double value); note that all variables must be declared before using out.

That is, double valuetest

 If (! Mydictionary. trygetvalue (Key, outvaluetest ))

   Valuetest = 0.0; // If such a key value does not existValuetest = 0.0, or assign the real value to valuetest

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.