【问题标题】:How to initialize an array of dictionaries [duplicate]如何初始化字典数组[重复]
【发布时间】:2014-09-23 22:56:38
【问题描述】:

我似乎无法正确理解此语法或任何具有此示例的资源。

我正在尝试类似的东西:

Row = new Dictionary<string, string> [] [{"A":"A"}, {"B":"B"}]

对这种语法有什么想法吗?

【问题讨论】:

  • 你想在这里创造什么?一个元素的数组?
  • @ChaseErnst 非常相似,但链接的线程不提供用于内部字典的集合初始值设定项。

标签: c# arrays dictionary initialization


【解决方案1】:

你可以试试这个方法:

Row = new Dictionary<string, string>[] 
{ 
    new Dictionary<string, string> { {"A", "A"} }, 
    new Dictionary<string, string> { {"B", "B"} } 
};

【讨论】:

    【解决方案2】:

    对数组和内部dictionaries使用collection initializers

    var arrayOfDictionaries = new Dictionary<string, string>[] 
    { 
        new Dictionary<string, string>() { {"A", "A"} }, 
        new Dictionary<string, string>() { {"B", "B"} } 
    };
    

    您可以在 MSDN herehere 上阅读有关此语法的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 2016-02-14
      • 2017-11-15
      • 1970-01-01
      • 2016-11-20
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多