Why does a static constructor not have any parameters?

前端 未结 9 1542
忘掉有多难
忘掉有多难 2021-02-07 03:02

Per MSDN:

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

A static constructor is called automatically to ini

相关标签:
9条回答
  • 2021-02-07 03:17

    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?

    0 讨论(0)
  • 2021-02-07 03:21

    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.

    0 讨论(0)
  • 2021-02-07 03:23

    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?

    0 讨论(0)
  • 2021-02-07 03:23

    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.

    0 讨论(0)
  • 2021-02-07 03:25

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

    A static constructor cannot be called directly.

    0 讨论(0)
  • 2021-02-07 03:28

    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.

    0 讨论(0)
提交回复
热议问题