概述
表示键\值对的集合,键不能为null,但值可以
添加
hanshtable.Add(“id”,“123”);
删除
hashtable.Clear();
hashtable.Remove(“id”);
遍历
与数组遍历相似,都可以使用foreach语句,由于Hashtable中的元素是一个键/值对,因此需要使用DictionaryEntry结构来遍历
DictionaryEntry结构表示一个键/值对的集合
namespace ConsoleApp1
{
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add("id", "A123");
hashtable.Add("name", "EM");
hashtable.Add("sex", "男");
Console.WriteLine("\t键\t值");
foreach (DictionaryEntry dicEntry in hashtable)
{
Console.WriteLine("\t" + dicEntry.Key + "\t" + dicEntry.Value);
}
Console.WriteLine();
}
}
}
键 值
id A123
sex 男
name EM
查找
ContainsKey\ContainsValue方法