- Home /
Question by
lachesis · Oct 27, 2013 at 07:58 PM ·
arraysforeachmultidimensional array
Returning one dimension of a two-dimensional array
Hi All,
I'd like a foreach loop to loop through each entry inside a certain dimension of a two-dimensional array. I'm using a multi-dimensional, non-jagged C# array of GameObjects, like this:
foreach(GameObject face in faceArray[cubeNumber, *])
What's the proper syntax for this?
Comment
Best Answer
Answer by Ejlersen · Oct 27, 2013 at 08:36 PM
Doing a foreach on a multidimensional array flattens it, so that wouldn't work. However, if you have an array of an array, then you could do it.
However, I would probably do something like this for columns:
for (int i = 0; i < yLength; i++)
{
// Do something with faceArray[cubeNumber, i];
}
For rows:
for (int i = 0; i < xLength; i++)
{
// Do something with faceArray[i, cubeNumber];
}
Thanks, Ejlersen. Ultimately, I used a for loop successfully, but a foreach just seemed cleaner. I didn't know that foreach flattened multidimensional arrays, though. Thanks!