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

                }

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

  • 相关阅读:
    怎样理解 C++ 11中的move语义
    面试---计算机网络
    TCP报头中的选项字段
    协程的实现之调度器
    CPU缓存
    TLS---线程局部存储
    微信libco协程设计及实现---总结
    libco源码解析---协程运行与基本结构
    2014年第五届蓝桥杯B组(C/C++)预赛题目及个人答案(欢迎指正)
    并查集(模板&典型例题整理)
  • 原文地址:https://www.cnblogs.com/gycnet/p/5728421.html
Copyright © 2011-2022 走看看