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
}
}