字典的用法,字典和字典集合的嵌套
static void Main(string[] args) { //字典有两个属性 key和value, //字典的好处是查找速度比list快 // //首先定义一个key为int 值为string的字典 Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "234"); dictionary.Add(3, "345"); Dictionary<int, Dictionary<int, string>> dic = new Dictionary<int, Dictionary<int, string>>(); //初始化 dic.Add(1, dictionary); //查找 Console.WriteLine("请输入你要查找字典的ID"); int i = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入你要查找数据的ID"); int index = Convert.ToInt32(Console.ReadLine()); if (dic.ContainsKey(i)) { if(dictionary.ContainsKey(index)) { // string str = dic[i][index]; Console.WriteLine("查找成功,值为{0}:", dic[i][index]); } else { Console.WriteLine("对不起,没有这个ID"); } } else { Console.WriteLine("对不起,没有这个ID"); } }