我有一个需要填充列表的字典。下面的代码不起作用。我该如何解决?
clientinfo.cs
public Dictionary<int, List<string>> Languages { get; set; }
main.cs
var ClientsData = new List<MapModel.ClientInfo> { }
ClientsData.Add(new MapModel.ClientInfo { Id = IDCounterDoctors, Languages = new Dictionary<int, List<string>>()});
ClientsData[0].Languages.Add(2376, ["english", "french"]); // issue is here
答案 0 :(得分:1)
您的列表初始化是错误的。还要确保您的语言属性已初始化
ClientsData[0].Languages = new Dictionary<int,List<string>>();
ClientsData[0].Languages.Add(2376,new List<string>(){ "english", "french"});
答案 1 :(得分:1)
你的代码有2个错误:
1.var ClientsData = new List {},这里应该是()not {};
2.ClientsData [0] .Languages.Add(2376,[“english”,“french”]);这里应该是一个新的List对象;
我编辑了你的代码,如下:
//initialize client data(list)
var ClientsData = new List<MapModel.ClientInfo>();
//add one ClientInfo into ClientData
ClientsData.Add(new MapModel.ClientInfo { Id = IDCounterDoctors,
Languages = new Dictionary<int, List<string>>()});
//set value to the first clientInfo's Language
ClientsData[0].Languages.Add(2376, new List<string>() { "english", "french" });