16
Reply

How can we pass parameters to Static Constructors?

Sahil Sharma

Sahil Sharma

10y
15.2k
0
Reply

    static constructor is called when the execution of the class is started. It is the first member to be execute in the class. at this time how can we pass any parameters to it. if we create object of the class then it will call the non static constructor. so we can not pass any parameters into it at any cost.

    Static constructor never have parameter.

    Static Constructors should not have any parameters.

    static constructor does not contain parameters.

    We cannot pass parameters to Static constructorReason: We cannot invoke Static constructor from the code, it will invoked automatically and only once, either on creation of first instance of the class or on accessing static member of the class

    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?

    your question is wrong...static constructor is a parameterless constructor. It will contain any parameters. bcz static constructor will invoke at the time of class is loading and you can't pass any parameters.static constructor is a parameterless constructor it should not have any access modifier

    We can only pass parameter to a non static constructor(parametrized constructor ) but we can not pass any parameter to a static constructor... Because access modifier(Public,Protected) is not allowed for static constructor

    static constructor is called when the execution of the class is started. It is the first member to be execute in the class. at this time how can we pass any parameters to it. if we create object of the class then it will call the non static constructor. so we can not pass any parameters into it at any cost.

    static constructor is called when the execution of the class is started. It is the first member to be execute in the class. at this time how can we pass any parameters to it. if we create object of the class then it will call the non static constructor. so we can not pass any parameters into it at any cost.

    Static constructor does not have access specifier and parameter. It called automatically when we try to access the class at first time. it will helpful to initialize the value the static field of the class.Note: The non static class also capable to have the static constructor. but static constructor only has the rights to access the static fields the that class.

    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.

    We can only pass parameter to a non static constructor(parametrized constructor ) but we can not pass any parameter to a static constructor... Because access modifier(Public,Protected) is not allowed for static constructor.

    static constructor is called when the execution of the class is started. It is the first member to be execute in the class. at this time how can we pass any parameters to it. if we create object of the class then it will call the non static constructor. so we can not pass any parameters into it at any cost.

    A static constructor is called automatically to initialize the class before the first instance is created, so we can't send it any parameters.

    You cant pass parameters to Static Constructors, because you cannot access any non-static member outside of a static method (constructor too).class Cons { public static string name = "Jagan";public string age;static Cons () {// you can access only static members [name] but NOT non-static [age] } }If your intention of passing parameter to Static Constructor is to set value of a Static variable, then you can directly set using class name. So thats why Static Constructor does not need arguments.