结构数组返回错误

本文关键字:错误 返回 数组 结构 | 更新日期: 2023-09-27 18:18:45

我有以下代码:

static int gridX = 40;
static int gridY = 40;
public struct CubeStruct
{
    public Transform cube;
    public bool alive;
    public Color color;
}
public CubeStruct cubeArray[,] = new CubeStruct[gridX, gridY];

返回以下错误:

错误CS1519:类、结构或接口中意外的符号','成员声明

错误CS0178: Invalid rank specifier: expected ,' or]'

错误CS1519:类、结构或接口中意外的符号';'成员声明

结构数组返回错误

在c#中,[,]位于变量名之前(即不像在C/c++中)。

public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];
public CubeStruct cubeArray[,] = new CubeStruct[gridX, gridY];
应:

public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];

在c#中,没有任何东西可以在Type之外浮动。所以你需要这样做:
还要注意,[,]出现在类型之后,而不是在标识符之后。
public class GridMain
{
    static int gridX = 40;
    static int gridY = 40;
    public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];
}
public struct CubeStruct
{
    public Transform cube;
    public bool alive;
    public Color color;
}

change:

public CubeStruct cubeArray[,] = new CubeStruct[gridX, gridY];

:

public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];