From the MSDN documentation on the Array.Copy
method:
When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).
I have always assumed that, in C#, two-dimensional arrays are laid out in row-major order (and similarly for higher dimensions), so I'm confused about the meaning of the parenthesized “or columns” in the above documentation. Does it mean that there may be circumstances where the .NET Framework uses column-major ordering?
It only shows that in C language, 2-D arrays are stored in row major order and thus iterating its elements in a row major order is more efficient. In languages like Pascal and Fortran, iterating by column major order will be more efficient because 2-D arrays are stored in column major order there.
The elements of an array can be stored in column-major layout or row-major layout. For an array stored in column-major layout, the elements of the columns are contiguous in memory. In row-major layout, the elements of the rows are contiguous. Array layout is also called order, format, and representation.
Row-major order is used in C/C++/Objective-C (for C-style arrays), PL/I, Pascal, Speakeasy, and SAS.
Accessing a column-major array in the column-row order will be efficient because the array is stored sequentially in this manner (and the CPU pre-fetches data required next). In the figure above this means the data would be accessed as 1, 4, 7.
I suspect it is merely trying to convey that an array is not a rectangle - it is a single linear space. Any concept of "row" or "column" is actually the invention of the user. There are no rows and no columns; any convention along the lines of arr[x,y]
is "row x, column y" or "column x, row y" is purely that: a convention; part of our imagination in conceptualizing something. The only real order is which index is navigated first. The first? or the last?
Whether you call that "rows" or "columns" is up to you, and there are examples of both in use in the wild.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With