C#中Dictionary几种遍历的实现代码
程序员文章站
2024-03-03 21:31:58
复制代码 代码如下: dictionary list=new dictionary
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]]);
}
复制代码 代码如下:
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]]);
}