Search code examples
c#javastatic-constructorstatic-block

Is a Java static block equivalent to a C# static constructor?


What is the real difference between a C# static constructor and a Java static block?

They both must be parameterless. They are both called only once, when the related class is first used.

Am I missing something, or are they the same thing, just with different names?


Solution

  • They are equivalent, except that a C# class can only have one static constructor (plus static field initializers).

    Also, in C#, a static constructor will apply the beforefieldinit flag.