C#嵌套词典

use*_*657 13 c# dictionary nested

我的语法有什么问题?我希望能够用这个获得"Genesis"的价值info["Gen"]["name"]

    public var info = new Dictionary<string, Dictionary<string, string>> {
    {"Gen", new Dictionary<string, string> {
    {"name", "Genesis"},
    {"chapters", "50"},
    {"before", ""},
    {"after", "Exod"}
    }},
    {"Exod", new Dictionary<string, string> {
    {"name", "Exodus"},
    {"chapters", "40"},
    {"before", "Gen"},
    {"after", "Lev"}
    }}};
Run Code Online (Sandbox Code Playgroud)

Moh*_*han 31

您无法使用定义类字段var.

更改varDictionary<string, Dictionary<string, string>>:

public Dictionary<string, Dictionary<string, string>> info =
    new Dictionary<string, Dictionary<string, string>>
    {
        {
            "Gen",
            new Dictionary<string, string>
            {
                {"name", "Genesis"},
                {"chapters", "50"},
                {"before", ""},
                {"after", "Exod"}
            }
        },
        {
            "Exod",
            new Dictionary<string, string>
            {
                {"name", "Exodus"},
                {"chapters", "40"},
                {"before", "Gen"},
                {"after", "Lev"}
            }
        }
    };
Run Code Online (Sandbox Code Playgroud)

有关关键字及其用法的更多信息,请参见此处var.