日期:2014-05-18  浏览次数:20834 次

如何循环获取dictionary泛型集合中的键和值
如何循环获取dictionary泛型集合中的键和值

------解决方案--------------------
Dictionary<string, Class1> cl = new Dictionary<string, Class1>();
Class1 s1 = new Class1("张三", 25);
Class1 s2 = new Class1("李四", 26);
Class1 s3 = new Class1("家六", 27);
cl.Add(s1.Name, s1);
cl.Add(s2.Name, s2);
cl.Add(s3.Name, s3);
foreach (KeyValuePair<string, Class1> a in cl)
{
MessageBox.Show(a.Value.Age.ToString() + " " + a.Value.Name.ToString());
}
foreach (KeyValuePair<string, Class1> a in cl)
{
if (a.Key == s1.Name)
MessageBox.Show(a.Value.Age.ToString() + " " + a.Value.Name.ToString());
}
------解决方案--------------------
C# code

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("a","aa");
            dic.Add("b", "bb");
            dic.Add("c", "cc");
            dic.Add("d", "dd");
            foreach (string  key in dic.Keys)
            {
                Console.WriteLine(dic[key]);
            }

------解决方案--------------------
C# code

        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("a", "aa");
        dic.Add("b", "bb");
        dic.Add("c", "cc");
        dic.Add("d", "dd");
        foreach (KeyValuePair<string, string> pair in dic)
        {
            //,pair.Key键,pair.Value值
            Response.Write(string.Format("{0}={1}<br>",pair.Key,pair.Value));
        }