I would like to iterate the rows and columns separately on a two dimensional array:
object[,] values;
How would I iterate through just the rows and just the columns?
I would like to iterate the rows and columns separately on a two dimensional array:
object[,] values;
How would I iterate through just the rows and just the columns?
Multi-dimensional arrays don't have rows and columns in the way you're referring to them - they just have several indexes used to access values. Iterating over such an array would be done using nested for-loops, and if you want to perform certain calculations on a per-dimension base you should alter the order of the loops accordingly.
Another option, if you only need to iterate over one dimension, is to use an array of arrays instead of a multi-dimensional array like this:
object[][] values;
It depends what's columns and rows for you but you could use this snippet of code:
for (int i = 0; i < values.GetLength(0); i++)
Console.WriteLine(values[i, 0]);
And:
for (int i = 0; i < values.GetLength(1); i++)
Console.WriteLine(values[0, i]);
Here's some code to iterate through the first and second dimensions of the array a 2 dimensional array. (There aren't really "rows" and "columns" because a multidimensional array can have any number of dimensions)
object[,] values = new object[5,5];
int rowIWant = 3; //Make sure this is less than values.GetLength(0);
//Look at one "row"
for(int i = 0; i < values.GetLength(1); i++
{
//Do something here with values[rowIWant, i];
}
int columnIWant = 2; //Make sure this is less than values.GetLength(1);
//Look at one "column"
for(int i = 0; i < values.GetLength(0); i++
{
//Do something here values[i, columnIWant];
}