17

What is wrong with my syntax? I want to be able to get the value "Genesis" with this 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"}
    }}};
7
  • 4
    And what compiler error do you get? Commented Mar 19, 2013 at 13:44
  • 2
    Do you get a compile error at the word var ..? Commented Mar 19, 2013 at 13:45
  • 3
    Aside from the actual cause of the error, why are you using a Dictionary<string, string> as the value anyway, when it appears you really want a simple Chapter class? Commented Mar 19, 2013 at 13:46
  • Yeah I think the issue is "var" because it is directly in a class and not used in a method. What should I use instead? Commented Mar 19, 2013 at 13:48
  • I agree with Jon Skeet, I think that creating a class would be the correct route and if he wanted a Collection then he could create a List<T> of the Class itself. Commented Mar 19, 2013 at 13:51

2 Answers 2

39

You cannot define a class field using var.

Change var to 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"}
            }
        }
    };

See here for more information about var keyword and its usage.

In C# 9, with the introduction of target-typed new expressions, you can rewrite the field definition as:

public Dictionary<string, Dictionary<string, string>> info = new()
{
    {
        "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"}
        }
    }
};
Sign up to request clarification or add additional context in comments.

Comments

3

From MSDN;

  • var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.

  • var cannot be used on fields at class scope.

  • Variables declared by using var cannot be used in the initialization expression.

Just change your var to Dictionary<string, Dictionary<string, string>>. Like;

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.