from / where我可以在类中调用静态类方法吗?

时间:2016-07-31 16:54:44

标签: c# oop

我在同一名称空间中有以下代码。

public static class usercls
{

        public static int testc()
    {

        int s=1;
    }
}

    public class User : Page
    {

        private static User user;     

        int s=usercls.testc();//why not accessible here?


    }

我无法访问课堂外的静态课程。有人可以帮我识别吗?

1 个答案:

答案 0 :(得分:3)

你好,函数testc()不会返回任何值。 看起来应该是这样的

public static int testc()
{
    int s=1;
    return s;
}

或者像这样

public static int testc()
{
    return 1;
}

之后你的代码应该编译。

其他类无法访问usercls的类函数,因为编译器没有编译它,因为出现错误,一旦修复了该错误,就可以从所有其他类访问它。

相关问题