简体   繁体   English

C#访问静态内部类

[英]C# access to static inside class

what word can i write to access static function inside class? 我可以写什么单词来访问类中的静态函数? like self:: in php? 喜欢自己::在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#. 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(); 如果您的静态类被命名为SampleClass,则可以使用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;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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