【发布时间】:2016-10-13 01:54:55
【问题描述】:
class Program
{
List<int> ages = new List<int>();
不能通过其他方法访问,请告诉我为什么? 我收到错误消息 cs0120“非静态字段、方法或属性‘program.ages’需要对象引用”
谢谢
【问题讨论】:
-
您如何尝试访问它?您描述的错误指向您未显示的代码行。提供一个完整的和最小的例子。
class Program
{
List<int> ages = new List<int>();
不能通过其他方法访问,请告诉我为什么? 我收到错误消息 cs0120“非静态字段、方法或属性‘program.ages’需要对象引用”
谢谢
【问题讨论】:
更改您的列表声明static 和public
class Program
{
public static List<int> ages = new List<int>();
// other method and stuff you want
}
【讨论】: