【发布时间】:2018-08-05 14:03:15
【问题描述】:
嘿,我正在尝试在 c# 中初始化一个数组,但不知道为什么这不起作用
Wave currentWave = new Wave(
{
{1, 1, 1},
{1, 1},
{1, 1, 1}
},
{3, 2}
);
private class Wave {
private int currentLoad;
private int[] aTimesToNextLoad;
private int[][] aEnemieLoads;
public Wave(int[][] aEnemiesToLoad, int[] aTimesToNextLoad) {
this.aEnemieLoads = aEnemieLoads;
this.aTimesToNextLoad = aTimesToNextLoad;
}
}
我得到了大约 18 个语法错误。我也试过使用new int[][] {...},然后我收到这条消息:
数组初始值设定项只能用于变量或字段初始值设定项。尝试改用新表达式
解决办法是什么?
【问题讨论】:
-
不相关,但
this.aEnemieLoads = aEnemieLoads是错误的 - 参数称为aEnemiesToLoad,您将字段分配给自身(查看编译警告)。
标签: c# arrays initialization