Is it possible to use the new System.Memory Span struct with two dimensional arrays of data?
double[,] testMulti =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 9.5f, 10, 11 },
{ 12, 13, 14.3f, 15 }
};
double[] testArray = { 1, 2, 3, 4 };
string testString = "Hellow world";
testMulti.AsSpan(); // Compile error
testArray.AsSpan();
testString.AsSpan();
Whilst testArray and testString have a AsSpan extension, no such extension exists for testMulti.
Is the design of Span limited to working with single dimensional arrays of data?
I've not found an obvious way of working with the testMulti array using Span.
You can create a
Spanwith unmanaged memory. This will allow you to Slice and Dice indiscriminately.Full Demo
Output
Other options would be to reallocate to a single dimension array, cop the penalty and do not Pass-Go
BlockCopymemcpydirectly and useunsafeand pointersCast<T>egmultiDimensionalArrayData.Cast<byte>().ToArray()The first 2 will be more performant for large arrays.