Indicate the initial capacity of each list that is stored in the list array

Hi, I want to create an instance of a list array using an array size of 1000 and the initial capacity of each list that is stored in an array of 500.

I try the following code,

static List<string>[] myList = new List<string>(500)[1000];     

this gives me an exception:

Cannot implicitly convert type 'string' to "System.Collections.Generic.List []

but if i just point

 static List<string>[] myList = new List<string>[1000];

no problem ... but with that I do not indicate the initial capacity of each list that is stored in the array.

In an array of lists, how to specify the initial capacity of each list stored in the array?

+4
source share
2 answers

. , :

static List<string>[] myList = new List<string>[1000];      

// in static constructor:
for(int i = 0; i<myList.Length; i++)
{
  myList[i] = new List<string>(500);
}
+4

, LINQ:

Enumerable.Range(0, 1000).Select(x => new List<string>(500)).ToArray();

, . :

class Foo
{
    private static List<string>[] myList;
    static Foo()
    {
        myList = Enumerable.Range(0, 1000)
            .Select(x => new List<string>(500)).ToArray();
    }
}
+1

Source: https://habr.com/ru/post/1537903/


All Articles