Dictionary 类提取键与值

 private void button1_Click(object sender, EventArgs e)
 {
            Dictionary<string, string> List = new Dictionary<string, string>();
            List.Add("1", "A");
            List.Add("2", "B");
            List.Add("3", "C");
            List.Add("4", "D");
            List.Add("5", "E");
            List.Add("6", "F");
            List.Add("7", "G");
            List.Add("8", "H");

            //方法一
            foreach (var item in List)
            {
                Console.WriteLine(item.Key + " , " + item.Value);
            }

            //方法二
            foreach (KeyValuePair<string, string> item in List)
            {
                Console.WriteLine(item.Key + " , " + item.Value);
            }

            //方法三
            foreach (string key in List.Keys)
            {
                Console.WriteLine(key + " , " + List[key]);
            }

  }

猜你喜欢

转载自www.cnblogs.com/YuanDong1314/p/8967734.html
今日推荐