Applying a hash table (hashtable) in C)

Source: Internet
Author: User
In. NET Framework, hashtable is a container provided by the system. Collections namespace. It is used to process and present key-value pairs similar to key/value. The key is usually used for quick search,
Keys are case sensitive. values are used to store values corresponding to keys. In hashtable, key/value pairs are of the object type, so hashtable can support any type of key/value pairs.


Add a key/value Key-value pair to the hash table: hashtableobject. add (Key, value); remove a key/value Key-value pair in the hash table: hashtableobject. remove (key); remove all elements from the hash table: hashtableobject. clear (); checks whether the hash table contains the key hashtableobject. contains (key); The following console program will include all the above operations: using system; using system. collections; // when using hashtable, you must introduce this namespace class hashtable {public static void main () {hashtable ht = new hashtable (); // create a hashtable instance ht. add ("e", "E"); // Add key/value pairs to ht. add ("A", "A"); ht. add ("C", "C"); ht. add ("B", "B"); string S = (string) HT ["A"]; If (HT. contains ("e") // determines whether the hash table contains a specific key. The return value is true or false console. writeline ("the e key: exist"); ht. remove ("C"); // remove a key/value pair from the console. writeline (HT ["A"]); // output
}
Dictionaryentry object is required to traverse a hash table. The Code is as follows:
For (dictionaryentry de in HT) // HT is a hashtable instance.
{
Console. writeline (De. Key); // de. Key corresponds to the key-Value Pair
Console. writeline (De. Value); // de. Key corresponds to the key/value Key-Value Pair Value
}
}

 

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.