Apply a hashtable in C # (Hashtable)

zhaozj2021-02-16  61

I. Brief introduction in .NET Framework, HashTable is a container provided by the System.Collections named space for processing and expressing key-value pairs like Key / Value, where KEY is usually available quickly, At the same time, Key is case sensitive; VALUE is used to store values ​​corresponding to Key. The key / value key value in HashTable is an Object type, so HashTable can support any type of Key / Value key value pair.

Second, the bricked table of the hash table adds a Key / Value key value in the hash table: HashTableObject.add (key, value); removes a Key / Value key value in the hash table: HashtableObject.remove Key); removes all elements from the hash table: HashTableObject.clear (); Judging whether the hash table contains a specific key Key: HashTableObject.contains (key); the following console will contain all the above operations: use system; using When using hashtable, you must introduce this namespace Class HashTable {public static void main () {uShtable HT = new hashtable (); // Create a HashTable instance HT.Add ("e", "e" ); // Add Key / Value key value to ht.add ("a", "a"); HT.Add ("c", "c"); ht.add ("b", "b"); String s = (string) HT ["a"]; if (HT.Contains ("e")) // Judging whether the hash table contains a specific key, its return value is true or false console.writeline ("The e key : exist "); ht.remove (" c "); // Remove a key / value key value to console.writeLine (HT [" A "]); // output a ht.clear (); / / Remove all Elements Console.Writeline (HT ["A"]); // will not have any output}} here

Third, traversing the hash table, traversing the hash table, you need to use the DictionaryEntry Object, the code is as follows: for (DictionaryTry de in HT) // HT is a HashTable instance {console.writeline (de.key); // de.key corresponds to Key / Value key value to key console.writeline (de.value); // de.key corresponds to the key / value key value pair value}

转载请注明原文地址:https://www.9cbs.com/read-22887.html

New Post(0)