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社区sC# 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
精彩评论