C#遍历Dictionary

C#遍历Dictionary方法

Dictionary<string, int> d = new Dictionary<string, int>();

foreach (KeyValuePair<string, int> pair in d)
{

     Console.WriteLine("{0}, {1}",
                pair.Key,
                pair.Value);
        }

Dictionary<string, int> list = new Dictionary<string, int>();
            //C#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]]);

            }

IDictionary dictionary = new Hashtable();
foreach (DictionaryEntry entry in dictionary)
{
   Object key = entry.Key;
    Object val = entry.Value;
}/* 何问起 hovertree.com */

推荐:http://www.cnblogs.com/roucheng/p/3521864.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值