简体   繁体   中英

C# access to static inside class

what word can i write to access static function inside class? like self:: in php?

You just use the type name:

static class Test
{

  public static string GetSomething()
  {
    return "Something";
  }

}

string s = Test.GetSomething();

If you're in the class already you just call the method.

只需使用StaticMethodName(...) (在定义静态方法的类中)或ClassName.StaticMethodName(...)

There is no such keyword in C#. You need to use the class name, eg

MyClass.StaticMember

Write the name of the class. For example:

public static class MyClass { 
public static void HelloWorld(){}
}

And use it like:

MyClass.HelloWorld();

If your static class is named etc. SampleClass, you can access its functions with SampleClass.YourFunction(); . If you want to call a function in other static method, just use the name of the function .

public class Discover 
{
    static int myVariable = 1;

    public Discover()
    {
      var test = myVariable;
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM