Dictionary 用法

邢法
2023-12-01
private void button1_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());
            }
        }



循环类型是: KeyValuePair
 类似资料: