【发布时间】:2020-03-02 20:25:42
【问题描述】:
我想创建一个由我用类定义的 "Highscore" 对象组成的数组。
当我尝试设置或读取特定数组内容的值时,我总是收到 NullReferenceException。
当我使用单个 Highscore 对象而不是数组时,它确实有效。
当我使用整数数组而不是高分数组时,它也可以工作。
代码
class Highscore
{
public int score;
}
class Program
{
static void Main()
{
Highscore[] highscoresArray = new Highscore[10];
highscoresArray[0].score = 12;
Console.WriteLine(highscoresArray[0].score);
Console.ReadLine();
}
}
System.NullReferenceException:
highscoresArray[] 为空。
【问题讨论】:
-
Highscore[] highscoresArray = Enumerable.Range(0, 10).Select(x => new Highscore()).ToArray();