• C#利用KeyValuePair实现Dictionary的遍历方法


    public class Class1
            {
                public string Name;
                public int Age;

                public Class1(string name, int age)
                {
                    this.Name = name;
                    this.Age = age;
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {
                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());
                }

                //本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/c/20110318/10072.html
            }

  • 相关阅读:
    strstr 函数的实现
    函数模板与模板函数
    内核态和用户态
    最短路径算法(跟新SPFA,Ford)
    P1042 乒乓球
    P2347 砝码称重
    P1087 FBI树
    P1540 机器翻译
    P1028 数的计算
    P1067 多项式输出
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2290202.html
Copyright © 2020-2023  润新知