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?

9
  • learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… Commented May 20, 2022 at 1:49
  • @diplomacynotwar Thanks for the link. These don't include a List of Objects. Commented May 20, 2022 at 2:01
  • Can this be reopened. These answers do not answer my question. Commented May 20, 2022 at 2:10
  • Can you clarify how an instance of, for example, StudentName is different to an instance of List<VariableList> in the context of instantiating a dictionary? That's in the answer here. Or how is a List<int> different to a List<VariableList> (example here)? 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. Commented May 20, 2022 at 2: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<int> is a list of ints vs List<VariableList> is a list of objects. Definitely not an obvious distinction for a C# newb. Commented May 20, 2022 at 2:14

1 Answer 1

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 ... }, ... }; 
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@SunwooYang thats exactly right, yeah

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.