0
点赞
收藏
分享

微信扫一扫

C#中的map集合

年迈的代码机器 2022-03-25 阅读 166
java后端

C#中的Directory<TKey,TValue>相当于Java中的Map集合

C# 中的map集合遍历取值方法如下:

 Dictionary<string, string> map = new Dictionary<string, string>();

 map.Add("1", "Chinese");
 map.Add("2", "Math");
 map.Add("3", "English");

 Console.WriteLine("Dictionary遍历方式:");
 Console.WriteLine("方法1:");
 foreach (var v in map )
 {
     Console.WriteLine("Key:" + v.Key + ";Value:" + v.Value);
 }

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

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

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

相关推荐

0 条评论