C#中Dictionary几种遍历的实现代码

时间:2022-09-17 23:00:24

代码如下:


 Dictionary<string,string> list=new Dictionary<string,string>;
//3.0以上版本
foreach(var item in list)
{
      Console.WriteLine(item.Key+item.Value);
}
//KeyValuePair<T,K>
foreach(KeyValuePair<string,string> kv in list)
{
      Console.WriteLine(kv.Key+kv.Value);
}
//通过键的集合取
foreach(string key in list.Keys)
{
      Console.WriteLine(key+list[key]);
}
//for循环遍历
List<string> test=new List<string>(list.Keys);
for(int i=0;i<list.Count;i++)
{
      Console.WriteLine(test[i]+list[test[i]]);
}