Dictionary<string, string[]> 的 C# 对象初始值设定项应该是什么样的?
What should a C# object initializer for a Dictionary<string, string[]> look like?
我正在尝试声明一个值为字符串数组的字典。我该怎么做?
我尝试了以下代码(不起作用):
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"}},
所以你的 class 可能看起来像:
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"}},
};
}
我正在尝试声明一个值为字符串数组的字典。我该怎么做?
我尝试了以下代码(不起作用):
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"}},
所以你的 class 可能看起来像:
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"}},
};
}