1 个带有二维数组错误的索引器

1 indexer with a 2D array error

假设我有一个这样的数组:

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

我怎么不能这样做,我得到的错误是:

Indexer has 2 parameters but is invoked with 1 argument 

谢谢 丹尼尔

您的 arr 数组是一个二维数组,想象一下网格。因此,您需要两个索引才能访问任何元素:行索引和列索引。

如果您正在尝试获取整行,这就是您正在尝试做的事情,那么您将需要像用户 pm100 所说的那样使用交错数组。

// 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];