0

I have a dictionary with the value being a list that contains VariableList objects like so

public VariableList {
    public string VariableName;
    public bool VariableBoolean;
}

Dictionary<string, List<VariableList>> myVariableDictionary;

How would I assign this Dictionary with multiple keys including all the objects in the List in one statement without using Add?

Sunwoo Yang
  • 1,213
  • 2
  • 12
  • 22
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-a-dictionary-with-a-collection-initializer – ProgrammingLlama May 20 '22 at 01:49
  • @diplomacynotwar Thanks for the link. These don't include a List of Objects. – Sunwoo Yang May 20 '22 at 02:01
  • Can this be reopened. These answers do not answer my question. – Sunwoo Yang May 20 '22 at 02:10
  • Can you clarify how an instance of, for example, `StudentName` is different to an instance of `List` in the context of instantiating a dictionary? That's in the answer [here](https://stackoverflow.com/a/28998723/3181933). Or how is a `List` different to a `List` ([example here](https://stackoverflow.com/a/414656/3181933))? If you can explain a substantial way that these are different, I'll gladly reopen your question, but there isn't really a difference here. – ProgrammingLlama May 20 '22 at 02:12
  • Because one is an object and the other is a list of objects. I'm pretty new to c# so I have no idea if the syntax for creating an object is the same as the syntax for creating a list of objects in a dictionary. And List is a list of ints vs List is a list of objects. Definitely not an obvious distinction for a C# newb. – Sunwoo Yang May 20 '22 at 02:14
  • Well, a list is a generic list meaning that it can contain the thing between `<` and `>`. So a `List` can contain integers, and a `List` can contain `VariableList` instances. The same is true of the value part for a dictionary. If it's a `Dictionary` then the value part can be an integer, if it's `Dictionary>` then the value part can be a list of integers, if it's `Dictionary>` then it can be a list of `VariableList`. – ProgrammingLlama May 20 '22 at 02:19
  • Why don't you try combining the list initializer with the dictionary initializer before saying that those answers aren't relevant to your question? – ProgrammingLlama May 20 '22 at 02:21
  • Because it's not immediately obvious for a c# noob. It may be obvious to you, but then a question and answer site wouldn't exist if it only existed for people who knew everything, right? I could also do without the snarky comments. If it confused me, then I can guarantee you others are also confused by this. – Sunwoo Yang May 20 '22 at 02:25
  • I've added another question to the list that more closely matches yours. – ProgrammingLlama May 20 '22 at 02:30

1 Answers1

1

Assuming you already have instances of your VariableList class, you can do something like this:

myVariableDictionary = new Dictionary<string, List<VariableList>>
{
    ["string1"] = new List<VariableList> { variableList1, variableList2 ... },
    ["string2"] = new List<VariableList> { variableList3, variableList4 ... },
    ...
};

MoonMist
  • 1,232
  • 6
  • 22
  • Thanks for your answer, Aggragoth! and if I didn't have instances of VariableList then I would just write new VariableList { VariableName="blah", VariableBoolean=true } in place of variableList1? – Sunwoo Yang May 20 '22 at 01:55
  • 1
    @SunwooYang thats exactly right, yeah – MoonMist May 20 '22 at 02:59