Use of the C # hash table

Source: Internet
Author: User

One, hash table (Hashtable) description in the. NET framework, Hashtable is a container provided by the System.Collections namespace for handling and performing similar key/value Key-value pairs, where key is usually used for quick lookups, while key is case-sensitive, and value is used to store values corresponding to key. Key/value Key-value pairs in Hashtable are all object types, so hashtable can support any type of key/value key-value pair. Two, simple operation of the hash table add a key to the Hashtable/Value key value pair: Hashtableobject.add (key,value), removing a key from the hash table/Value Key-value pair: Hashtableobject.remove (key); Removes all elements from the hash table: Hashtableobject.clear (); Determines if a hash table contains a specific key: Hashtableobject.contains (key); The following console program will contain all of the above actions:usingSystem;usingSystem.Collections;//This namespace must be introduced when using HashtableclassHashtable { Public Static voidMain () {Hashtable HT=NewHashtable ();//Create a Hashtable instanceHt. ADD ("E","e");//adding Key/value Key-value pairsHt. ADD ("A","a"); Ht. ADD ("C","C"); Ht. ADD ("B","b");stringS= (string) ht["A"]; if(HT. Contains ("E"))//determines whether the hash table contains a specific key with a return value of TRUE or falseConsole.WriteLine ("The E key:exist"); Ht. Remove ("C");//Remove a Key/value key-value pairConsole.WriteLine (ht["A"]);//output a hereHt. Clear ();//Remove all elementsConsole.WriteLine (ht["A"]);//there will be no output here}} Three, traversing a hash table traversing a hash table requires the use of DictionaryEntry Object, which is as follows: for(DictionaryEntry deinchHt//HT is a Hashtable instance{Console.WriteLine (DE. Key);//de. Key corresponds to the Key/value key value pair keyConsole.WriteLine (DE. Value);//de. Key corresponds to the value of the Key/value key} Four, sort the hash table to sort the hash table here is the definition of key/the key in the value key pair is rearranged according to certain rules, but in practice this definition is not possible because we cannot rearrange the key directly in the Hashtable, and if we need to Hashtable provide some sort of output of the rule, we can use a workaround: ArrayList Akeys=NewArrayList (HT. Keys);//don't forget to import the System.CollectionsAkeys. Sort ();//Sort by alphabetical order for(stringSkeyinchAkeys) {Console.Write (Skey+":"); Console.WriteLine (Ht[skey]);//post-order output}

Use of the C # hash table

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.