I create a list of list:
List<List<string>> C = new List<List<string>>();
Then assign some values to it. Assume the assignment is as follows:
List<string> B = new List<string>();
for (int j = 0; j < x; j++)
{
B.Clear();
for (int i = 0; i < y; i++)
B.Add(i.ToString());
C.Add(B);
}
x and y vary at each run of the code. So, I do not know them beforehand. (At this point of the code, I do not know how to find the size of C. I mean x and y, not x*y.)
I would like to assign/copy C to a 2D array, D[,]. Before the declaration of D, size of D (i.e., rows and columns that correspond to x and y) should have been calculated in some way by using C. How can I make the assignment?
You can't just assign a jagged array value to a 2D array because these structures store data differently. A jagged array is an array each element of which stores a reference to another array, while a two-dimensional array is a series of elements stored in memory. See also "Multidimensional Arrays (C# Programming Guide)" article.
Also, you cannot simply assign the value of a list of lists to a variable of two-dimensional array or jagged array type, because these are different data types that require an explicit conversion.
So you need to copy each element to the new array.