Gad*_*uks 2 c# dictionary object-initializers
我试图声明一个值为字符串数组的字典.我怎样才能做到这一点?
我尝试了以下代码(不起作用):
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"}},
}
Run Code Online (Sandbox Code Playgroud)
您需要指定您的值是如下数组:
使用隐式类型数组
{"Key_0", new[] {"Value_0.0", "Value_0.1"}},
Run Code Online (Sandbox Code Playgroud)
或者明确指定类型
{"Key_0", new string[] {"Value_0.0", "Value_0.1"}},
Run Code Online (Sandbox Code Playgroud)
所以你的班级看起来像:
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"}},
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
136 次 |
| 最近记录: |