This is basically a restatement of this question: Java: Multi-dimensional array vs. One-dimensional but for C#.
I have a set amount of elements that make sense to store as a grid. Should I use a array[x*y] or a array[x][y]?
EDIT: Oh, so there are one dimensional array[x*y], multidimensional array[x,y] and jagged array[x][y], and I probably want jagged?
A one-dimensional array stores a single list of various elements having a similar data type. A two-dimensional array stores an array of various arrays, or a list of various lists, or an array of various one-dimensional arrays. It represents multiple data items in the form of a list.
One Dimension: Once you connect two points, you get a one-dimensional object: a line segment. A line segment has one dimension: length. Two Dimensions: A flat plane or shape is two-dimensional. Its two dimensions are length and width.
Two – dimensional array is the simplest form of a multidimensional array. We can see a two – dimensional array as an array of one-dimensional array for easier understanding.
A One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array.
There are many advantages in C# to using jagged arrays (array[][]
). They actually will often outperform multidimensional arrays.
That being said, I would personally use a multidimensional or jagged array instead of a single dimensional array, as this matches the problem space more closely. Using a one dimensional array is adding complexity to your implementation that does not provide real benefits, especially when compared to a 2D array, as internally, it's still a single block of memory.
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