C# 要列出的词典词典

C# 要列出的词典词典,c#,C#,我需要创建一个名字列表,这些名字是字典的关键。对我来说,它看起来完全正常,但我有几个错误。 结构必须像 {paperony=>{西红柿,1},{胡萝卜,4},素食者=>{西红柿,4}, {土豆,6}} List成分=新列表(); 添加(新字典() { { “帕波罗尼”, { {“西红柿”,1}, {“胡萝卜”,4} } }, { “素食者”, { {“西红柿”,4}, {“土豆”,6} } } } ); 您需要使用字典的显式初始化。比如说, ingredients.Add(new Diction

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

{paperony=>{西红柿,1},{胡萝卜,4},素食者=>{西红柿,4}, {土豆,6}}

List成分=新列表();
添加(新字典()
{
{
“帕波罗尼”,
{
{“西红柿”,1},
{“胡萝卜”,4}
}
},
{
“素食者”,
{
{“西红柿”,4},
{“土豆”,6}
}
}
}
);

您需要使用字典的显式初始化。比如说,

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}
                    }
                }
            }

        );
components.Add(新字典()
{
{
“帕波罗尼”,
新词典{
{“西红柿”,1},
{“胡萝卜”,4}
}
},
{
“素食者”,
新词典{
{“西红柿”,4},
{“土豆”,6}
}
}
}
);
如果要避免使用显式初始化,可以使用以下方法

ingredients.Add(new Dictionary<string, Dictionary<string, int>>()
            {
                ["Paperoni"] = {
                                ["Tomatoes"]= 1,
                                ["Carrot"]= 4
                                },
                ["Vegetarian"] = 
                                {
                               ["Tomatoes"]= 4,
                               ["Potatoes"]= 6
                                }
            }

        );
components.Add(新字典()
{
[“Paperoni”]={
[“西红柿”]=1,
[“胡萝卜”]=4
},
[“素食者”]=
{
[“西红柿”]=4,
[“土豆”]=6
}
}
);

您需要使用字典的显式初始化。比如说,

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}
                    }
                }
            }

        );
components.Add(新字典()
{
{
“帕波罗尼”,
新词典{
{“西红柿”,1},
{“胡萝卜”,4}
}
},
{
“素食者”,
新词典{
{“西红柿”,4},
{“土豆”,6}
}
}
}
);
如果要避免使用显式初始化,可以使用以下方法

ingredients.Add(new Dictionary<string, Dictionary<string, int>>()
            {
                ["Paperoni"] = {
                                ["Tomatoes"]= 1,
                                ["Carrot"]= 4
                                },
                ["Vegetarian"] = 
                                {
                               ["Tomatoes"]= 4,
                               ["Potatoes"]= 6
                                }
            }

        );
components.Add(新字典()
{
[“Paperoni”]={
[“西红柿”]=1,
[“胡萝卜”]=4
},
[“素食者”]=
{
[“西红柿”]=4,
[“土豆”]=6
}
}
);

是的,应该有一个您创建的dictionary实例
{“Paperoni”,新dictionary{{“西红柿”,1},…}
在处理嵌套字典/泛型时,我更喜欢使用类型别名:
使用InnerCollection=dictionary
使用MiddleCollection=字典
列表
;在gerenal的coruse中,我尽量避免筑巢。特别是两层深老实说,这看起来更像是某种适当数据结构的XML或JSON表示,然后在代码中使用。是的,在处理嵌套字典/泛型时,应该有一个dictionary实例,您可以创建
{“Paperoni”,newdictionary{{“西红柿”,1},…}
,我更喜欢使用类型别名:
使用InnerCollection=Dictionary
使用MiddleCollection=字典
列表
;在gerenal的coruse中,我尽量避免筑巢。特别是两层深老实说,这看起来更像是某种适当数据结构的XML或JSON表示,然后是您在代码中使用的。