如何在 c#
中的 non static
方法中调用 static
方法?
面试官给我的情景:
class class1
{
public static void method1(){}
public void method2()
{
//call method1()
}
我们该怎么做
最佳答案
通常的做法是用类名调用静态方法。
参见:Static Classes and Static Class Members (C# Programming Guide)
The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.
所以你的电话会是这样的:
class1.method1();
但是没有必要
您可以调用没有类名的静态方法,例如:
method1();
但是你只能在拥有那个静态方法的类中这样做,你不能在那个类之外调用没有类名的静态方法。
关于c# - C#中如何在非静态方法中调用静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532756/