排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
.net
1、初始化
Dictionary<int,string> myDictionary =newDictionary<int,string>();
2、添加元素
myDictionary.Add(1,"C#"); myDictionary.Add(2,"C++"); myDictionary.Add(3,"ASP.NET"); myDictionary.Add(4,"MVC");myDictionary.Add(5,"Core");
3、通过Key查找元素
if(myDictionary.ContainsKey(1)) { Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]); }
4、遍历元素
foreach(KeyValuePair<int,string>kvp in myDictionary) { Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value); }
5、仅遍历键 Keys
Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys; foreach(intkeyinkeyCol){ Console.WriteLine("Key = {0}", key); }
6、仅遍历值 Valus属性
Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values; foreach(stringvalueinvalueCol) { Console.WriteLine("Value = {0}", value); }
7、通过Remove方法移除指定的键值
myDictionary.Remove(1); if(myDictionary.ContainsKey(1)) { Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]); } else { Console.WriteLine("不存在 Key : 1"); }
评价