0

How to convert List<int[]> to int[,] where all arrays in the list are int[2]?

I'm trying:

List<int[]> list = new List<int[]>();
list.Add(new int[2] { 3, 4 });
int[,] arr = list.ToArray();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
gsivanov
  • 123
  • 2
  • 3
  • 2
    Possible duplicate of [How to convert list of arrays into a multidimensional array](https://stackoverflow.com/questions/9774901/how-to-convert-list-of-arrays-into-a-multidimensional-array) – Dominik Szymański Nov 15 '17 at 08:14

1 Answers1

7

You can't convert List to int[,] via LINQ.

You need to create new array [N, 2] and fill in a loop:

var arr = new int[list.Count, 2];

for (var i = 0; i < list.Count; i++)
{
    arr[i, 0] = list[i][0];
    arr[i, 1] = list[i][1];
}
Backs
  • 24,430
  • 5
  • 58
  • 85