'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?
Solution 1:[1]
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?
Solution 2:[2]
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?
Solution 3:[3]
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.
Solution 4:[4]
Because you can't call it directly (as per MSDN):
A static constructor cannot be called directly.
Solution 5:[5]
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?
Solution 6:[6]
- 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.
Solution 7:[7]
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");
Solution 8:[8]
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.
Solution 9:[9]
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
Solution 10:[10]
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.
Solution 11:[11]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow