开发者

Static Operation like java in c#

In java you can add static operation block in any class and it will be called when the application start:

class test{
   static{
    //do some operation when the application starts.
   }
}

What is the equivalent in c#?

Thank开发者_StackOverflow社区s


C# has the static constructor:

class Test {
    static Test() {
        // …
    }
}


The equivalent in C# is the static constructor:

class Test
{ 
   static Test()
   { 
    //do some operation before accessing to any member of the class
   } 
} 

The static constructor is guaranteed to be executed before any class member is accessed. It's not guaranteed to be called at application start though.


It's called a static constructor:

class test
{
    static test()
    {
        //do some operation when the application starts.
    }
}


Use static constructor

class test
{
    static test()
    {
        // do some job
    }
}


If I recall properly it's not easy, you have to resort to static contructors. Try to take a look here Microsoft documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜