[.NET] 如何利用 for loop 寻访 Dictionary (Linq)

如题,有时候并无法使用foreach去得到Dictionary中的key,value

可使用Linq转成List,然后使用for loop取得key value

 

Dictionary<int, string> myDict = new Dictionary<int, string>() { { 2, "This" }, { 1, "is" }, { 5, "radio" }, { 12, "clash" }, }; var target = myDict.ToList(); for ( int index=0; index < target.Count; index++ ) Console.WriteLine( "Key: {0,2} Value : {1}", target[index].Key, target[index].Value ); /* Output Key: 2 Value : This Key: 1 Value : is Key: 5 Value : radio Key: 12 Value : clash */

你可能感兴趣的:(.net,String,list,LINQ,Dictionary)