Categories
arrays c# list type-conversion

Fast way to convert a two dimensional array to a List ( one dimensional )

I have a two dimensional array and I need to convert it to a List (same object). I don’t want to do it with for or foreach loop that will take each element and add it to the List. Is there some other way to do it?

To convert double[,] to List<double>, if you are looking for a one-liner, here goes

double[,] d = new double[,]
{
{1.0, 2.0},
{11.0, 22.0},
{111.0, 222.0},
{1111.0, 2222.0},
{11111.0, 22222.0}
};
List<double> lst = d.Cast<double>().ToList();


But, if you are looking for something efficient, I’d rather say you don’t use this code.

Please follow either of the two answers mentioned below. Both are implementing much much better techniques.