字典(dictionary)的声明
声明字典时,需同时声明字典内的键和值的类型。
1
   | Dictionary<int,string> dic = new Dictionary<int,string>();
   | 
 
字典的读取与增加
键和值可以是任何类型,但键必须是唯一的。
1 2 3 4 5 6 7 8 9
   | class Program {     static void Main(string[] args) {         Dictionary<int,string> dic = new Dictionary<int,string>();         dic.Add(1, "hi");         dic.Add(2, "你好");         dic[3] = "hello";         Console.WriteLine(dic[3]);     } }
  | 
 
字典初始化器
1 2 3 4
   | Dictionary<int,string> dic = new Dictionary<int, string> {             {1,"hello world" },             {2,"字典的基本用法" }         };
  | 
 
dic.Remove(key)
用于移除键对应的成员,返回bool,成功为true,失败为false。
1 2 3 4 5 6 7 8
   | class Program {     static void Main(string[] args) {         Dictionary<int,string> dic = new Dictionary<int,string>();         dic.Add(1, "hi");         dic.Add(2, "你好");         dic.Remove(1);     } }
  |