class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> myDictionary = new Dictionary<string, string>();

            myDictionary.Add("No1", "Zhang San");
            myDictionary.Add("No2", "Li Si");
            myDictionary.Add("No3", "Wang Wu");

            Console.WriteLine("Dictionary遍历方式:");

            Console.WriteLine("方法1:");
            //3.0版本以上
            foreach (var v in myDictionary)
            {
                Console.WriteLine("Key:" + v.Key + ";Value:" + v.Value);
            }

            Console.WriteLine("方法2:");
            foreach (KeyValuePair<string, string> kvp in myDictionary)
            {
                Console.WriteLine("Key:" + kvp.Key + ";Value:" + kvp.Value);
            }

            Console.WriteLine("方法3:");
            foreach (string key in myDictionary.Keys)
            {
                Console.WriteLine("Key:" + key + ";Value:" + myDictionary[key]);
            }

            Console.WriteLine("方法4:");
            foreach (string value in myDictionary.Values)
            {
                Console.WriteLine("Value:"+value);
            }

            Console.Read();
        }
    }

 

相关文章:

  • 2021-12-18
  • 2021-08-27
  • 2021-07-21
  • 2021-11-27
  • 2022-12-23
  • 2021-11-30
  • 2021-08-15
猜你喜欢
  • 2022-12-23
  • 2021-09-16
  • 2021-08-20
相关资源
相似解决方案