2017-04-05 98 views
0

属性我真是一个初学者,遇到过这个问题,我解决不了甚至花小时后搜索互联网。我不明白为什么堆栈对象Grades始终为空,每次运行应用程序时,都会抛出“System.NullReferenceException”异常。当我将鼠标悬停在我的鼠标上档次,它说:“场‘Student.Grades’从未分配,将永远有它的默认值空”。我也不能使用“{get; set;}”来访问这个属性。那么谁能告诉我为什么?非常感谢!(第一次要求对堆栈溢出问题,遗憾的坏格式)C#不能建立一个堆栈<T>在C#中

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++; 
    } 
} 

回答

0

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

this.Grades =新的堆栈();

代码。

类在C#与空默认值。