C#中的Directory<TKey,TValue>相当于Java中的Map集合
C# 中的map集合遍历取值方法如下:
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("1", "Chinese");
map.Add("2", "Math");
map.Add("3", "English");
Console.WriteLine("Dictionary遍历方式:");
Console.WriteLine("方法1:");
foreach (var v in map )
{
Console.WriteLine("Key:" + v.Key + ";Value:" + v.Value);
}
Console.WriteLine("方法2:");
foreach (KeyValuePair<string, string> kvp in map )
{
Console.WriteLine("Key:" + kvp.Key + ";Value:" + kvp.Value);
}
Console.WriteLine("方法3:");
foreach (string key in map.Keys)
{
Console.WriteLine("Key:" + key + ";Value:" + myDictionary[key]);
}
Console.WriteLine("方法4:");
foreach (string value in map.Values)
{
Console.WriteLine("Value:"+value);
}