Dictionary class to extract keys and values

 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");

            // Method one 
            foreach ( var item in List)
            {
                Console.WriteLine(item.Key + " , " + item.Value);
            }

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

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

  }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324992116&siteId=291194637