c# 无法将 Stack<T> 设置为 class 中的 属性

c# cannot set up a Stack<T> as a property in class

我真的是 c# 的初学者,遇到了这个问题,即使花了几个小时在互联网上搜索也无法解决。我不明白为什么堆栈对象 Grades 总是空的,每次我 运行 应用程序时,它都会抛出异常 "System.NullReferenceException"。当我将鼠标悬停在成绩上时,它会显示 "field 'Student.Grades' is never assigned to and will always have its default value null"。我也无法使用“{get;set;}”来访问此 属性。那么谁能告诉我为什么?非常感谢!!!(第一次问堆栈溢出问题,格式不好)

class Student : Person
{

    public int studentID { get; set; }
    public static int nextID;
    public Stack<int> Grades;

    public Student(string firstName, string lastName, DateTime birthDate, 
           string address1, string address2, string city, string state, 
           string zip, string country)
    {
        FirstName = firstName;
        LastName = lastName;
        BirthDate = birthDate;
        Address1 = address1;
        Address2 = address2;
        City = city;
        State = state;
        Zip = zip;
        Country = country;
        //track the number of students enrolled
        studentID = nextID;
        nextID++;
    }
}

在您的构造函数中,您可以添加

this.Grades = 新堆栈();

代码。

Class 在 c# 中具有 null 默认值。