您的位置:首页 > 编程语言 > C#

C# Dictionary 的几种遍历方法

2014-05-20 15:16 459 查看
Dictionary<string, int>
list = new Dictionary<string, int>();



list.Add("d", 1);



//3.0以上版本

foreach (var item in list)

{

Console.WriteLine(item.Key + item.Value);

}

//KeyValuePair<T,K>

foreach (KeyValuePair<string, int>
kv in list)

{

Console.WriteLine(kv.Key + kv.Value);

}

//通过键的集合取

foreach (string key in list.Keys)

{

Console.WriteLine(key + list[key]);

}

//直接取值

foreach (int val in list.Values)

{

Console.WriteLine(val);

}

//非要采用for的方法也可

List<string>
test = new List<string>(list.Keys);



for (int i
= 0; i < list.Count; i++)

{

Console.WriteLine(test[i] + list[test[i]]);

}



如果有不足之处,请指出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: