【问题标题】:How to initialize an dictionary with an array value?如何用数组值初始化字典?
【发布时间】:2016-11-20 19:37:43
【问题描述】:

我有以下代码:

public static Dictionary<string, string[]> dict = new Dictionary<string, string[]>() {
    "key1", { "value", "another value", "and another" }
};

这是不正确的。错误列表包含以下内容:

“Add”方法没有重载需要 3 个参数

没有给出与'Dictionary.Add(string, string[])'的所需形参'value'相对应的参数

我基本上只是想用预设值初始化我的字典。不幸的是,我不能使用代码初始化,因为我在一个静态类中工作,其中只有变量。

我已经尝试过这些东西:

  • ... {"key1", new string[] {"value", "another value", "and another"}};
  • ... {"key", (string[]) {"value", "another value", "and another"}};

但我没有运气。任何帮助表示赞赏。

PS:如果我使用两个参数,日志会显示can't convert from string to string[]

【问题讨论】:

    标签: c# arrays dictionary


    【解决方案1】:

    这对我有用(围绕着另一组 {} - 用于您创建的 KeyValuePair)所以它找不到您尝试执行的功能:

    Dictionary<string, string[]> dict = new Dictionary<string, string[]>
    {
        { "key1", new [] { "value", "another value", "and another" } },
        { "key2", new [] { "value2", "another value", "and another" } }
    };
    

    我建议遵循 C# {} 约定 - 良好的缩进有助于轻松找到这些问题 :)

    【讨论】:

    • string[] 不是必需的。 C# 编译器从初始化列表推断类型。
    【解决方案2】:

    您必须将{} 中的每个键值对都包围起来,您可以将new[]{...} 用于string[]

    public static Dictionary<string, string[]> dict = new Dictionary<string, string[]>()
    {
        { "key1", new[]{ "value", "another value", "and another" }}
    };
    

    【讨论】:

      【解决方案3】:

      字典中的每个条目都应该用 {} 括起来,键值对应该用 , 分隔

      public static Dictionary<string, string[]> dict = new Dictionary<string, string[]>() 
      {
          { "key1", new string[] { "value", "another value", "and another" } },
          { "key2", new string[] { "value", "another value", "and another" } },
      };
      

      如果您使用的是 C# 6,则可以利用新语法:

      public static Dictionary<string, string[]> dict = new Dictionary<string, string[]>() 
      {
          ["key1"] = new string[] { "value", "another value", "and another" },
          ["key2"] = new string[] { "value", "another value", "and another" }
      };
      

      【讨论】:

        【解决方案4】:

        这对于数组初始化无效。这将是匿名类型的失败尝试:

        string[] array = new { "a", "b" }; // doesn't compile
        

        您还缺少一些大括号:

        public static Dictionary<string, string[]> dict = new Dictionary<string, string[]>()
        {
            { "key1", new []{ "value", "another value", "and another" } }
        };
        

        或者:

        public static Dictionary<string, string[]> dict = new Dictionary<string, string[]>()
        {
            ["key1"] = new []{ "value", "another value", "and another" }
        };
        

        【讨论】:

          【解决方案5】:

          试试这个:

          Dictionary<string, string[]> dict = new Dictionary<string, string[]>() 
          {
              { 
                  "key1", new string[] { "value", "another value", "and another" }
              } 
          };
          

          【讨论】:

            猜你喜欢
            • 2014-09-23
            • 1970-01-01
            • 2018-10-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-08-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多