如何构造Dictionary <string,List <string >>的实例

Bow*_*eng 1 c# object-initializers

我试图用C#编写代码,但我发现我不能像C++这样构造一个实例:

Dictionary<string, List<string>> FirSet =
    new Dictionary<string, List<string>>() {
        { "a", {"ab", "abc"} },
        { "b", {"bc", "bcd"} }
    };
Run Code Online (Sandbox Code Playgroud)

小智 6

您必须在字典中启动列表 new List<string>()

它应该如何工作:

Dictionary<string, List<string>> FirSet = new Dictionary<string, List<string>>()
{
    { "a", new List<string> { "ab", "abc" }},
    { "b", new List<string> { "bc", "bcd" }}
};
Run Code Online (Sandbox Code Playgroud)

  • 由于我们的答案(现在)相同,我已经删除了我的并且赞成你的. (2认同)