C # nested dictionaries

What is wrong with my syntax? I want to get the meaning of "Being" with thisinfo["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"}
    }}};
+5
source share
2 answers

You cannot define a class field with var.

Change varto Dictionary<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"}
            }
        }
    };

For more information about the varkeyword and its use, see here .

+22
source

From MSDN;

  • var can only be used when declaring and initializing a local variable in the same statement; a variable cannot be initialized to zero either to a group of methods or an anonymous function.

  • var cannot be used in fields in a class.

  • , var, .

var Dictionary<string, Dictionary<string, string>>. ;

public Dictionary<string, Dictionary<string, string>> info =
    new Dictionary<string, Dictionary<string, string>>{}
+1

All Articles