We have the following object
int [,] oGridCells;
which is only used with a fixed first index
int iIndex = 5;
for (int iLoop
I'd be suprised if it were possible: I bet oGridCells[iIndex, iLoop]
is just a sort of shorthand (internally, in MSIL) for oGridCells[iIndex * iLoop]
, and that multidimensional arrays are syntactic sugar for this.
To answer your question: No. You will have to loop the values.
You can try this:
int[,] twoD = new int[2,2];
twoD[0, 0] = 1;
twoD[0, 1] = 2;
twoD[1, 0] = 3;
twoD[1, 1] = 4;
int[] result = twoD.Cast<int>().Select(c => c).ToArray();
The result will be an integer array with data:
1, 2, 3, 4
You cannot get a reference to each array. You can, however, use a jagged array.
"But if the array was being heavily manipulated then a single dimension array would be more efficient than a multi dimension array."
I did some profiling of exactly this last summer and was surprised to see no significant difference in performance between a 2D and 1D array.
I didn't test the performance of a jagged array.
The following code demonstrates copying 16 bytes (4 ints) from a 2-D array to a 1-D array.
int[,] oGridCells = {{1, 2}, {3, 4}};
int[] oResult = new int[4];
System.Buffer.BlockCopy(oGridCells, 0, oResult, 0, 16);
You can also selectively copy just 1 row from the array by providing the correct byte offsets. This example copies the middle row of a 3-row 2-D array.
int[,] oGridCells = {{1, 2}, {3, 4}, {5, 6}};
int[] oResult = new int[2];
System.Buffer.BlockCopy(oGridCells, 8, oResult, 0, 8);
Edit:
I realized there is a way! Granted, it's probably not worth it. Use unsafe code. Full example, showing both ways, with unsafe below:
public class MultiSingleUnsafe
{
public static unsafe void Main(String[] a)
{
int rowCount = 6;
int iUpperBound = 10;
int [,] oGridCells = new int[rowCount, iUpperBound];
int iIndex = rowCount - 2; // Pick a row.
for(int i = 0; i < iUpperBound; i++)
{
oGridCells[iIndex, i] = i;
}
for (int iLoop = 0; iLoop < iUpperBound; iLoop++)
{
//Get the value from the 2D array
int iValue = oGridCells[iIndex, iLoop];
Console.WriteLine("Multi-dim array access iValue: " + iValue);
//Do something with iValue
}
fixed(int *lastRow = &(oGridCells[iIndex,0]))
{
for (int iLoop = 0; iLoop < iUpperBound; iLoop++)
{
int iValue = lastRow[iLoop];
Console.WriteLine("Pointer access iValue: " + iValue);
}
}
}
}
There is no way I know to cast a multiple-dimensional array into a single-dimensional one in C#. Of course, you could create a new single-dimensional array and copy into it. But I don't think this will get a performance benefit even if you loop over the values multiple times. As Daren said, internally it's all pointer arithmetic anyway. If you want to be certain, profile it.