I think this might be a pretty simple question, but I haven’t been able to figure it out yet. If I’ve got a 2-dimensional array like so:
int[,] array = new int[2,3] { {1, 2, 3}, {4, 5, 6} };
What’s the best way to iterate through each dimension of the array with a nested foreach statement?
If you want to iterate over every item in the array as if it were a flattened array, you can just do:
which would print
If you want to be able to know the x and y indexes as well, you’ll need to do:
Alternatively you could use a jagged array instead (an array of arrays):
or