0

lets say I have an array like this:

 byte[,] arr = new byte[4,4];
 byte[] x = arr[0]; // error here

how come I cannot do this, the error im getting:

Indexer has 2 parameters but is invoked with 1 argument 

Thanks Daniel

dgamma3
  • 2,333
  • 4
  • 26
  • 47
  • you need jagged arays https://msdn.microsoft.com/en-us/library/2s05feca.aspx – pm100 Feb 06 '15 at 01:49
  • 2
    What isn't clear about the error? You have a 2d array, if you want to index at an element, you need to specify two parameters. – Preston Guillot Feb 06 '15 at 01:50
  • See http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays – artm Feb 06 '15 at 01:53

1 Answers1

0

Your arr array is a two-dimensional array, so imagine a grid. Therefore, you need two indices in order to access any element: row index and column index.

If you're trying to get an entire row, which is what it looks like you're trying to do, then you will need to use staggered arrays as user pm100 said.

// Initialize array size
byte[][] arr = new byte[4][];
for (int i = 0; i < arr.GetLength(0); i++)
    arr[i] = new byte[4];

// Grab the first row
byte[] x = arr[0];
Reticulated Spline
  • 1,892
  • 1
  • 18
  • 20