C#比较私有静态和公共静态方法

Vik*_*ram 5 c#

在C#中,标记的public static方法和标记为的方法之间有什么区别private static

他们是如何分配和访问的?

Tyl*_*eat 8

私有静态方法只能在其定义的类中访问.可以在类外部访问公共静态方法.

public class MyClass
{ 
    private static void MyPrivateMethod()
    {
        // do stuff
    }

    public static void MyPublicMethod()
    {
        // do stuff
    }
}

public class SomeOtherClass
{
    static void main(string[] args)
    {
         MyClass.MyPrivateMethod(); // invalid - this method is not visible

         MyClass.MyPublicMethod(); // valid - this method is public, thus visible
    }
}
Run Code Online (Sandbox Code Playgroud)

就内存分配而言,请参见此处:

方法存储在内存中的哪个位置?