您可以通过以下三种方式之一枚举字典:
使用KeyValue对
Dictionary<int, string> dict = new Dictionary<int, string>(); foreach(KeyValuePair<int, string> kvp in dict) { Console.WriteLine("关键: " + kvp.Key.ToString() + ", 值: " + kvp.Value); }
使用按键
Dictionary<int, string> dict = new Dictionary<int, string>(); foreach(int key in dict.Keys) { Console.WriteLine("关键: " + key.ToString() + ", 值: " + dict[key]); }
使用价值
Dictionary<int, string> dict = new Dictionary<int, string>(); foreach(string s in dict.Values) { Console.WriteLine("值: " + s); }