First, start with the namespace
System.Collections
Then get to the point.
Common methods and properties of Hashtable:
01: Common Properties
Property name |
Description |
Count |
Gets the number of key/value pairs contained in the Hashtable |
Keys |
Gets the collection of keys contained in the Hashtable |
Values |
Gets the collection that contains the values in the Hashtable |
02: Common Methods
return value type |
Method name |
Description |
void |
ADD (Object key,object value) |
Adds the specified key and the merit element to the Hashtable |
void |
Remove (Object Key) |
Remove an element with a specific key from the Hashtable |
void |
Clear () |
Remove all elements from the Hashtable |
Well, the light says no practice fake bashi, on the code
Class program { static void Main (string[] args) { //Create a Hashtable instance Hashtable ht=new Hashtable () ; Add the KeyValue key value to ht. ADD ("A", "1"); Ht. ADD ("B", "2"); Ht. ADD ("C", "3"); Ht. ADD ("D", "4"); Traversing a hash table can only be traversed with foreach, because Hashtable cannot be accessed with an index
Traverse key
foreach (Object item in HT. Keys)
{
Console.WriteLine (string) item);
}
Traverse value
foreach (Object item in HT. Value)
{
Console.WriteLine (string) item);
}
foreach (DictionaryEntry de in HT) {Console.WriteLine ("Key--{0}; Value--{1}. ", De. Key, DE. Value); }
//hash table sort ArrayList akeys=new ArrayList (ht. Keys); Akeys. Sort (); foreach (string skey in Akeys) {Console.WriteLine ("{0, -15} {1, -15}", Skey, Ht[skey]); }//Determines if the hash table contains a specific key with a return value of TRUE or False if (HT. Contains ("a")) Console.WriteLine (ht["a"); Assign a value to the corresponding key ht["A"] = "Hello"; Removes a KeyValue key value pair ht. Remove ("C"); Traversal hash table foreach (DictionaryEntry de in HT) {Console.WriteLine ("Key--{0}; Value--{1}. ", De. Key, DE. Value); }//Remove all element ht. Clear (); There will be no output Console.WriteLine (ht["A"]); Console.readkey (); }}
Source: https://www.cnblogs.com/hfddz/p/6534235.html
Usage of Hashtable in C #