我正在尝试声明一个值为字符串数组的字典。我该怎么做?
我尝试了以下代码(不起作用):
Dictionary<string, string[]> NewDic = new Dictionary<string,string[]>
{
{"Key_0", {"Value_0.0", "Value_0.1"}},
{"Key_1", {"Value_1.0", "Value_1.1", "Value_1.2"}},
}
最佳答案
您需要指定您的值是数组,如:
{"Key_0", new[] {"Value_0.0", "Value_0.1"}},
或者明确指定类型
{"Key_0", new string[] {"Value_0.0", "Value_0.1"}},
所以你的类看起来像:
public static class NewClass
{
private static Dictionary<string, string[]> NewDic = new Dictionary<string, string[]>
{
{"Key_0", new[] {"Value_0.0", "Value_0.1"}},
{"Key_1", new string[] {"Value_1.0", "Value_1.1", "Value_1.2"}},
};
}
关于c# - Dictionary<string, string[]> 的 C# 对象初始值设定项应该是什么样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29395415/