C# c语言中的java静态操作#
在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.
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也是如此。