键值对集合(类似Hashtable)
创建Dictionary字典类型:
Dictionary<string, int> dic = new Dictionary<string, int>();
方法
1、添加
dic.Add()
dic["name"] = 1
2、遍历Dictionary
static void Main(string[] args)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("name", 1);
dic["age"] = 20;
// KeyValuePair是dic的每一个键值对,可以通过kv.Key来获取键,kv.Value来获取值
foreach (KeyValuePair<string, int> kv in dic)
{
Console.WriteLine($"键:{kv.Key},值:{kv.Value}");
}
Console.ReadLine();
}