Introduction of a hash table (Hashtable)
In the. NET framework, Hashtable is a container provided by the System.Collections namespace for handling and performing key-value pairs that resemble Key/value, where key is often used for quick lookup and key is case-sensitive ; value is used to store the value corresponding to the key. Key/value key value pairs in Hashtable are of type object, so hashtable can support any type of Key/value key-value pairs.
Second, the simple operation of a hash table
Adds a Key/value key value pair to the hash table: Hashtableobject.add (Key,value);
Removes a Key/value key value pair in a hash table: Hashtableobject.remove (key);
Remove all elements from the hash table: Hashtableobject.clear ();
Determines whether a hash table contains a specific key key:HashtableObject.Contains (key);
The following console program will contain all of the above actions:
using System;
using System.Collections; //使用Hashtable时,必须引入这个命名空间
class hashtable
{
public static void Main()
{
Hashtable ht=new Hashtable(); //创建一个Hashtable实例
ht.Add("E","e");//添加key/value键值对
ht.Add("A","a");
ht.Add("C","c");
ht.Add("B","b");
string s=(string)ht["A"];
if(ht.Contains("E")) //判断哈希表是否包含特定键,其返回值为true或false
Console.WriteLine("the E key:exist");
ht.Remove("C");//移除一个key/value键值对
Console.WriteLine(ht["A"]);//此处输出a
ht.Clear();//移除所有元素
Console.WriteLine(ht["A"]); //此处将不会有任何输出
}
}