Why does a static constructor not have any parameters?

Viewed 23084

Per MSDN:

A static constructor does not take access modifiers or have parameters.

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

A static constructor cannot be called directly.

Can any one please explain why can't the static constructor have parameters?

11 Answers

A Static Constructor is called implicitly by CLR automatically and it is the first block of code to run under a class. We cannot pass any parameters to the static constructors because these are called implicitly and for passing parameters, we have to call it explicitly which is not possible

For More Clarification Refer This-enter link description here

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

A static constructor cannot be called directly.

Static constructors can't be called explicitly and hence don't take any parameters. Let's assume that static constructors can have the parameters passed. Now, because we can't call the constructors explicitly, this means that any parameter that is needed to be passed has to be given in the constructor definition itself. e.g.

class Sample
{
    int a;
    int b;
    static Sample(int a1=10, int b1=20)
    {
        this.a = a1;
        this.b = b1;
    }
}

If you give it a thought, this definition has no relevance because the values could have been directly assigned inside the constructor as below.

class Sample
{
    int a;
    int b;
    static Sample()
    {
         this.a = 10;
         this.b = 20;
    }
}

In a way you can say that implicit calling mechanism by CLR is the reason why static constructors can't have parameters. The calls being implicit wouldn't allow us to pass varying values to constructors, and hence the purpose of having parameters is nullified.

Related