C # hashtable usage Summary

Source: Internet
Author: User

Hashtable: a set of key/value pairs. These key/value pairs are organized according to the hash code of the key. In hashtable, keyValue pairs are of the object type, so hashtable can support any type of keyValue pairs.

Operations on the hash table include:

1. Add a keyValue pair

2. traverse the hash table

3. Sort hash tables

4. Determine whether a hash table contains a specific key. The return value is true or false.

5. Remove a keyValue pair

6. output values in the hash table

7. Remove all elements

 

 

Hashtable ht = new hashtable (); // 1. Add a keyValue pair to ht. add ("D", "DDD"); ht. add ("A", "AAA"); ht. add ("C", "CCC"); ht. add ("B", "BBB"); // 2. traverse the hash table foreach (dictionaryentry de in HT) {console. writeline ("key: {0}, value: {1}", de. key, de. value);} // 3. Sort the hash table. arraylist akeys = new arraylist (HT. keys); akeys. sort (); foreach (string key in akeys) {console. writeline ("sorted key: {0}, value: {1}", key, HT [Key]);} // 4. Determine whether the hash table contains a specific key, the return value is true or false if (HT. contains ("D") console. writeline (HT ["D"]); // 5. Remove a keyValue pair ht. remove ("C"); // 6. Output AAA console here. writeline (HT ["A"]); // 7. Remove all elements ht. clear (); // 8. No output console exists. writeline (HT ["A"]);

  

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.