简体   繁体   中英

Why does a static constructor not have any parameters?

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?

As MSDN says, A static constructor is called automatically to initialize the class before the first instance is created . Therefore you can't send it any parameters.

If the CLR must call a static constructor how will it know which parameters to pass it?

Static constructors are called automatically as part of type initialization. They're not called explicitly... so there's nowhere you could provide any arguments to correspond to the constructor parameters. Why would you want to allow parameters if you could never specify any values for the arguments?

How would you control the arguments that were passed to such a constructor, given that it's invoked automatically by the run-time when the class is referenced for the first time, and can't be called directly?

In theory, such a syntax could have been devised and implemented, but then that would necessitate its direct invocation, since now a simple class reference won't know what to pass in as arguments to it. The whole point of the static constructor is to perform type-level initializing prior to using the type. Doing so automatically ensures that this is the case, whereas direct invocation leaves plenty of room for mistakes.

Because you can't call it directly (as per MSDN):

A static constructor cannot be called directly.

A static constructor couldn't have any parameters. Well I suppose it could theoretically - but there is no instance of the class so it wouldn't make any sense. What would you do with those parameters if you had them? Call other static methods?

  • Static Constructor is called automatically before first instance of the class is created.
  • Declared by prefixing a static keyword to the constructor definition.
  • It can not not take access modifiers or have any parameters.

Make an empty constructor to the static class, and put the parametrized code to a normal function. If you call this function, the static class will be created.

the static class:

static class DataB
{
    static DataB(){}

    public static void funcWithParams(string st)
    {...}
}

you can create it like this:

DataB.funcWithParams("some string");

Static Constructor

Because static constructor invoke automatically (we does not have any control over calling of static constructor) that's why we can't pass parameter to static constructor.

And if we can not pass parameter to static constructor, then why we will create static constructor as parameterized.

So, we must have parameter less static constructor.

Here is an example of a method for allowing nested classes to access Form controls WITHOUT PASSING THE FORM AS A PARAMETER TO THE NESTED CLASS' CONSTRUCTOR:

public partial class Form1 : Form
{
    public int nWow;

    public Form1()
    {
        InitializeComponent();
        Inner.AssignMe(this); // This is where the real action is.
    }

    class Inner
    {
        static Form1 Me;

        static Inner(){} // empty static constructor necessary

           // Called AssignMe in the Form1 constructor in this code, 
           // but this can be generalized to any nested class.
        public static void AssignMe(Form1 form) { Me = form; }

        public Inner() { Me.nWow = 1; } // Now u can access public Form1
    }                        // members and methods even from the nested
}                            // class' constructor.

I figured this out based on user3567816's message above, which, though terse and having 0 votes, is never the less by far the most elegant solution and very unique. No one else is giving this advise to this kind of question. NO MORE BUTT UGLY REDUNDANT FORM PARAMETERS IN CONSTRUCTORS OF NESTED CLASSES! This is absolutely brilliant!!

I couldn't help but give a VB.Net twist with the use of the static variable name Me. Smirk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM