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