如何在C#中访问静态方法? [英] How to Access a static method in c#?

查看:593
本文介绍了如何在C#中访问静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在类中有一个静态方法时,它只访问静态成员,然后 静态方法只能使用类名进行访问.因此,我无法在示例中访问静态方法:

When we have a static method in a class it access only static members right and the static method can access only with class name. So I am not able to access the static method in my example:

class myclass
{
    int i  ; static int j ;
    static void get()
    {
        j = 101;
        Console.WriteLine(j.ToString ());
    }
    public void test()
    {
        i = 11; j = 12;
        Console.WriteLine(i.ToString());
        Console.WriteLine(j.ToString());
    }
}
class Program
{
    static void Main(string[] args)
    {
        myclass clsmyclas = new myclass();
        clsmyclas.test();

        Console.ReadLine();
    }
}

}

推荐答案

您应将其更改为

public static void get() 

并使用

myclass.get();

不是该类的实例.

这篇关于如何在C#中访问静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆