Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "111");
dict.Add(2, "222");
//判断是否存在相应的key并显示
if (dict.ContainsKey(2))
{
Console.WriteLine(dict[2]);
}
//遍历Keys
foreach (var item in dict.Keys)
{
Console.WriteLine("Key:{0}", item);
}
//遍历Values
foreach (var item in dict.Values)
{
Console.WriteLine("value:{0}", item);
}
//遍历整个字典
foreach (var item in dict)
{
Console.WriteLine("key:{0} value:{1}", item.Key, item.Value);
} |