【问题标题】:Cant declare a list as a global variable? [duplicate]不能将列表声明为全局变量? [复制]
【发布时间】:2016-10-13 01:54:55
【问题描述】:
class Program
{
    List<int> ages = new List<int>();

不能通过其他方法访问,请告诉我为什么? 我收到错误消息 cs0120“非静态字段、方法或属性‘program.ages’需要对象引用”

谢谢

【问题讨论】:

  • 您如何尝试访问它?您描述的错误指向您未显示的代码行。提供一个完整的最小的例子。

标签: c# console-application


【解决方案1】:

更改您的列表声明staticpublic

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

   // other method and stuff you want
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多