Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中的java静态操作#_C#_Java_Static - Fatal编程技术网

C# c语言中的java静态操作#

C# c语言中的java静态操作#,c#,java,static,C#,Java,Static,在java中,您可以在任何类中添加静态操作块,并在应用程序启动时调用它: class test{ static{ //do some operation when the application starts. } } c#中的等价物是什么? 谢谢C#拥有: 它被称为静态构造函数: class test { static test() { //do some operation when the application starts.

在java中,您可以在任何类中添加静态操作块,并在应用程序启动时调用它:

class test{
   static{
    //do some operation when the application starts.
   }
}
c#中的等价物是什么?
谢谢

C#拥有:


它被称为静态构造函数:

class test
{
    static test()
    {
        //do some operation when the application starts.
    }
}
class Test
{ 
   static Test()
   { 
    //do some operation before accessing to any member of the class
   } 
} 
使用静态构造函数

class test
{
    static test()
    {
        // do some job
    }
}
C#中的等价物是静态构造函数:

class test
{
    static test()
    {
        //do some operation when the application starts.
    }
}
class Test
{ 
   static Test()
   { 
    //do some operation before accessing to any member of the class
   } 
} 

静态构造函数保证在访问任何类成员之前执行。但不能保证在应用程序启动时调用它。

如果我能正确地回忆起来,这并不容易,您必须求助于静态构造函数。 试着看一下这里

事实上,它保证不会在应用程序启动时被调用,只有在第一次访问该类时才会被调用。但据我所知,Java也是如此。