Let's say initialized a two-dimensional array
string[,] arr = { {"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}, {"10", "11", "12"} }; Is it possible to somehow access the array string as a one-dimensional array?
Let's say initialized a two-dimensional array
string[,] arr = { {"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}, {"10", "11", "12"} }; Is it possible to somehow access the array string as a one-dimensional array?
No, you can't do that.
But you can write a wrapper that will "look" like a one-dimensional array. For example:
public struct ArrayRow<T> { private readonly T[,] array; private readonly int row; public ArrayRow(T[,] array, int row) { this.array = array; this.row = row; } public int Length { get { return array.GetLength(1); } } public T this[int column] { get { return array[row, column]; } set { array[row, column] = value; } } } But a similar structure, although it looks like an array, cannot be transferred instead of an array.
However, if you add code, you can make this structure implement the IList<T> interface, which the arrays implement too - this will allow you to use the same code for working with regular arrays and with similar cuts.
Multidimensional arrays are stored in memory as one continuous one-dimensional array. When accessing by the index [i, j] , a specific position in this internal one-dimensional array is calculated and the value is returned.
Since there is no internal representation in the form of separate arrays, it is impossible to turn to them. The only way to get a one-dimensional array is to create it manually and copy the values there, but it will be a copy of the array, with individual elements.
a[x] = myObj , but a[x] = myObj through properties / methods, for example a[x].myProp = 18 ); but in this case, you still need to first create a separate copy of a piece of a multidimensional array - LanorkinAnd this option is not good?
string[][] arr = new string[][] { new string[] {"1", "2", "3"}, new string[] {"4", "5", "6"}, new string[] {"7", "8", "9"}, new string[] {"10", "11", "12"} }; Source: https://ru.stackoverflow.com/questions/446642/
All Articles