我已经收到过几次这个问题,我想知道为什么我不能在 switch 中使用我的索引,我收到一个错误,即 array[index] = null,有什么原因吗?
// array of classes, im using public getters and setters to access the rank and cardcolor
Cardgame[] cardgame = new Cardgame[50];
int index = 0;
string CardColor = "";
...
for (int i = 0; i < 4; i++)
{
switch (i)
{
case 1: CardColor = "red";
break;
case 2: CardColor = "blue";
break;
case 3: CardColor = "diamond";
break;
case 4: CardColor = "candy!";
break;
}
for (int x = 0; x <= 13; x++)
{
index++;
Cardgame[index].Color = CardColor;
switch (x)
{
default: Cardgame[index].Number = x.ToString();
break;
case 11: Cardgame[index].Number = "Farmer";
break;
case 12: Cardgame[index].Number = "Queen";
break;
case 13: Cardgame[index].Number = "King";
break;
}
}