开发者

Static Operation like java in c#

开发者 https://www.devze.com 2023-04-11 16:37 出处:网络
In java you can add static operation block in any class and it will be called when the application start:

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

精彩评论

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

关注公众号