Apply a hashtable in C # (Hashtable)

xiaoxiao2021-03-06  109

Applying a hashtable in the C #, a HashTable is briefly described in .NET Framework, HashTable is a container provided by System.Collections namespace for processing and expressing key-value similar to Key / Value. For, Key is usually available to quickly find, while Key is case sensitive; VALUE is used to store values ​​corresponding to Key. HashTable The key / value key value is Object type, so HashTable can support any type of Key / Value key value pair. Second, the briche 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 pair in a hash table: HashTableObject.remove (key); removes all elements from the hash table: HashTableObject.clear (); judgment hash table Whether to include a specific key key: HashTableObject.Contains (key); judgment whether the hash table contains a specific value value: HashTableObjectContainSvalue The following console programs will contain all the above: 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 key value 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, 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 () here (); // Remove all Elements Console.Writeline (HT [" A "]); // will not have any output}} here

Third, traversing the hash table to use the hash table to use DictionaryTry Object, the code is as follows: Foreach (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-103569.html

New Post(0)