Since you're dealing with one-dimensional arrays, you can simply use the Length Property to get the length of the first dimension:
int[][] grid = new int[10][];
for (int x = 0; x < grid.Length; x++)
{
grid[x] = new int[10];
}
(Using the GetLength Method works as well:)
int[][] grid = new int[10][];
for (int x = 0; x < grid.GetLength(0); x++)
{
grid[x] = new int[10];
}
The problem with your code is that you're calling grid.GetUpperBound(1)
where grid
is a one-dimensional array -- it doesn't have a second dimension (index 1) that you could get the upper bound of.
Your GetXY method should look like this:
public int GetXY(int x, int y)
{
if (x < 0 || x >= grid.Length)
{
throw ...
}
int[] items = grid[x];
if (y < 0 || y >= items.Length)
{
throw ...
}
return items[y];
}
Note that jagged arrays are not magic that makes your code faster – measure if they actually do!