當前位置:成語大全網 - 新華字典 - C# 怎麽從緩存字符數組列中找匹配的值

C# 怎麽從緩存字符數組列中找匹配的值

我看了下,妳試圖用List<string[]>這樣的數據結構來存儲”姓名<->號碼“這樣的鍵值對,其實更合適的做法是使用C#中的Dictionary,妳的代碼可以寫成下面的樣子:

Dictionary<String,?String>?peopleNumberDict?=?new?Dictionary<string,?string>();

peopleNumberDict.Add("小明",?"12345");

peopleNumberDict.Add("小紅",?"952654");

peopleNumberDict.Add("小李",?"56626");

Console.WriteLine(peopleNumberDict["小紅"]);

Console.WriteLine(peopleNumberDict["小李"]);

Console.WriteLine(peopleNumberDict["小明"]);

在這段代碼中,構造了壹個用姓名做key,用號碼做Value的字典,在需要用姓名取到對應的號碼的時候,直接寫peopleNumberDict["小紅"]就可以了。