字典词典列表

时间:2019-11-17 12:47:57

标签: c#

我需要创建一个名称列表,这些名称是字典的关键。对于我来说,看起来完全可以,但是我有几个错误。 结构必须像

  

{paperony => {Tomatoes,1},{Carrot,4},素食主义者=> {Tomatoes,4},   {土豆,6}}

List<Dictionary<string, Dictionary<string, int>>> ingredients = new List<Dictionary<string, Dictionary<string, int>>>();
ingredients.Add(new Dictionary<string, Dictionary<string, int>>()
            {
                {
                    "Paperoni", 
                    {
                        {"Tomatoes", 1},
                        {"Carrot", 4}
                    }
                },

                {
                    "Vegetarian",
                    {
                        {"Tomatoes", 4},
                        {"Potatoes", 6}
                    }
                }
            }

        );

1 个答案:

答案 0 :(得分:2)

您需要使用Dictionary的显式初始化。例如,

<FormText />

如果要避免使用显式初始化,可以使用以下方法。

ingredients.Add(new Dictionary<string, Dictionary<string, int>>()
            {
                {
                    "Paperoni", 
                    new Dictionary<string, int>{
                        {"Tomatoes", 1},
                        {"Carrot", 4}
                    }
                },

                {
                    "Vegetarian",
                    new Dictionary<string, int>{
                        {"Tomatoes", 4},
                        {"Potatoes", 6}
                    }
                }
            }

        );