D.Dave
D.Dave

Reputation: 77

Initialize an array of lists of a list with c#

I am trying to initialize an array of lists of a list declared as a

List <List<int>>[] a

The problem here is that I do not know the syntax (I even do not know if the above syntax is correct). I assume it to be correct because to initialize an array of lists declared as b, following syntax is used:

List<int>[] b = 
{
    new List<int> { 1, 3, 4 }, 
    new List<int> { 3, 4, 12 }
};

Can anyone help me here?

Upvotes: 0

Views: 3653

Answers (2)

Technivorous
Technivorous

Reputation: 1712

List is a dynamic array try to stick to one or the other.

List<int[]> a = new List<int[]>(); is a list of int arrays, and is acceptable

but you could just have List<int> intlist = new List<int>();

and you can also do List<List<int>> listoflistsofints = new List<List<int>>()

and then listoflistsofints.Add(intlist);

all perfectly viable. the reason to use list and not array is that the size is dynamic, and you can use .Sort() methods to re-order the list, and adding and replacing elements is easy:

intlist.Add(153);

i hope this clears it up for you, let me know if you need to know more!

Upvotes: 1

JSteward
JSteward

Reputation: 7091

You have an array of lists of list of int:

List<List<int>>[] a = new List<List<int>>[]
{
    new List<List<int>>()
    {
        new List<int>(){1,2,3,4,5},
        new List<int>(){10,20,30,40,50}
    },
    new List<List<int>>()
    {
        new List<int>(){6,7,8,9},
        new List<int>(){60,70,80,90}
    }
};

Upvotes: 2

Related Questions