Convert byte[,] to byte[]

后端 未结 4 619
后悔当初
后悔当初 2021-01-15 22:32

Does anyone know of an efficient way to flatten a 2d array (non-jagged) in C# to a 1d and back again. I know in the back end C# must hold onto it as a 1d array I would just

4条回答
  •  一个人的身影
    2021-01-15 23:18

    var myFlattenedByteArray = my2DByteArray.SelectMany(b=>b).ToArray();
    

    This will return each element of each subarray of the 2D byte array in subarray-element order: myFlattenedByteArray[1] == my2DByteArray[0][1]. There are probably more performant or efficient solutions (especially the slupring of the Enumerable produced by SelectMany into its own Array) but it's a one-liner.

提交回复
热议问题