C# Dictionary 的几种遍历方法


Dictionary<string, int> list = new Dictionary<string, int>();
 
"d", 1);
 
//3.0以上版本
foreach (var item in
            {
Console.WriteLine(item.Key + item.Value);
            }
//KeyValuePair<T,K>
foreach (KeyValuePair<string, int> kv in
            {
Console.WriteLine(kv.Key + kv.Value);
            }
//通过键的集合取
foreach (string key in
            {
Console.WriteLine(key + list[key]);
            }
//直接取值
foreach (int val in
            {
Console.WriteLine(val);
            }
//非要采用for的方法也可
List<string> test = new List<string>(list.Keys);
 
for (int
            {
Console.WriteLine(test[i] + list[test[i]]);
            }

如果有不足之处,请指出!