2017-04-05 38 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#與空默認值。