Public constructor and static constructor

Strider007 picture Strider007 · Jun 8, 2010 · Viewed 10.2k times · Source

I am reading a code in C# that uses two constructors. One is static and the other is public. What is the difference between these two constructors? And for what we have to use static constructors?

Answer

Konrad Rudolph picture Konrad Rudolph · Jun 8, 2010

static and public are orthogonal concepts (i.e. they don’t have anything to do with each other).

public simply means that users of the class can call that constructor (as opposed to, say, private).

static means that the method (in this case the constructor) belongs not to an instance of a class but to the “class itself”. In particular, a static constructor is called once, automatically, when the class is used for the first time.

Furthermore, a static constructor cannot be made public or private since it cannot be called manually; it’s only called by the .NET runtime itself – so marking it as public wouldn’t be meaningful.