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)