C#中dictionary的使用

在C#中,Dictionary提供快速的基于键值的元素查找。

他的结构是:Dictionary<[key], [value]> ,当有很多元素的时候可以使用它。使用前,必须声明它的键类型和值类型,如下。

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

添加数据

if (!pList.ContainsKey("Item1"))            
  pList.Add("Item1", "ZheJiang");

删除数据

myDictionary.Remove(key);

根据Key获取VALUE

String value=dictionary[Key];

遍历数据

 foreach (var node in dictionary)
  {
  //...
  }

遍历key

foreach (var key in dictionary.Keys)
    {
        Console.WriteLine("Output Key: {0}", key);
    }

遍历value

foreach (String value in dictionary.Values)
    {
        Console.WriteLine("Output Value: {0}", value);
    }

 


posted @ 2016-08-16 14:17  等等等风来  阅读(800)  评论(0编辑  收藏  举报