zoukankan      html  css  js  c++  java
  • C# Dictionary 的几种遍历方法

    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]]);

                }

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

  • 相关阅读:
    codevs 2149 矩形周长
    codevs 3044 矩形面积求并
    codevs 1293 送给圣诞夜的极光
    codevs 2806 红与黑
    codevs 1536 海战
    codevs 1262 不要把球传我
    codevs 2606 约数和问题
    BZOJ 2301 problem b
    BZOJ 3994 约数个数和
    codevs 1173 最优贸易
  • 原文地址:https://www.cnblogs.com/gycnet/p/5728421.html
Copyright © 2011-2022 走看看