What is the correct syntax for populating a dictionary with nested lists as a value?

advertisements

I am new to C# and I am trying to define a Dictionary having:

as key:

a string

as value:

a List of Lists of strings.

What I could came up (not entirely sure it's right?) is this:

var peopleWithManyAddresses = new Dictionary<string, List<List<string>>> {};

Now, if the above is right, I would like to know how to populate one item of peopleWithManyAddresses.

Intellisense is telling me that the following is correct only up until "Lucas":

peopleWithManyAddresses.Add("Lucas", { {"first", "address"}, {"second", "address"} });

What's the correct syntax for it?

P.S. I know I could use a class, but for learning purposes I'd like to do it this way for now.


To initialize the List<List<string>> object, you have to use the new List<List<string>> { ... } syntax. And to initialize each sub list you have to use a similar syntax, i.e. new List<string> {... }. Here is an example:

var peopleWithManyAddresses = new Dictionary<string, List<List<string>>>();

peopleWithManyAddresses.Add("Lucas", new List<List<string>>
{
    new List<string> { "first", "address" },
    new List<string> { "second", "address" }
});