.net 遍历Dictionary两种方式

来源:互联网 发布:mysql不等于优化 编辑:程序博客网 时间:2024/06/17 13:24

//统计字符串中字符出现次数,不区分大小写            string str = "Welcome to Chinaword";            string lowerstr=str.ToLower();            Dictionary dic = new Dictionary();            foreach(char ch in lowerstr)            {                if (dic.ContainsKey(ch))                {                    dic[ch]++;                }                else                {                    dic[ch] = 1;                }            }//方法一:直接遍历Keys.            foreach(char ch in dic.Keys)            {               Console.WriteLine("{0}出现的次数为{1}",ch,dic[ch]);                        }//方法二:遍历dic中的键值对            foreach (KeyValuePair ch in dic)            {                Console.WriteLine("{0}出现的次数为{1}",ch.Key,ch.Value);            }


原创粉丝点击