2016-10-12 35 views
-2
class Program 
{ 
    List<int> ages = new List<int>(); 

不能訪問裏面的其他方法,請告訴我爲什麼? 我遇到錯誤信息cs0120「A對象引用需要非靜態字段,方法或屬性‘program.ages’」無法將列表聲明爲全局變量?

感謝

+0

你如何*嘗試*訪問它?您描述的錯誤指向您未顯示的一行代碼。提供*完整*和*最小*示例。 – David

回答

1

更改您的列表聲明staticpublic

class Program 
{ 
    public static List<int> ages = new List<int>(); 

    // other method and stuff you want 
}