为什么静态构造函数没有任何参数

时间:2022-09-25 12:36:55

As per MSDN:

根据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 the static constructor can not have parameters.

任何人都可以解释为什么静态构造函数不能有参数。

9 个解决方案

#1


24  

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.

正如MSDN所说,在创建第一个实例之前,会自动调用静态构造函数来初始化类。因此您无法发送任何参数。

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

如果CLR必须调用静态构造函数,它将如何知道传递它的参数?

#2


8  

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?

静态构造函数作为类型初始化的一部分自动调用。它们没有被明确调用...所以你无处可提供任何与构造函数参数相对应的参数。如果您永远不能为参数指定任何值,为什么还要允许参数?

#3


8  

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.

从理论上讲,这样的语法本来是可以设计和实现的,但是那时需要直接调用,因为现在一个简单的类引用不会知道要传递给它的参数。静态构造函数的要点是在使用类型之前执行类型级初始化。这样做会自动确保这种情况,而直接调用会留下足够的错误空间。

#4


4  

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

因为您无法直接调用它(根据MSDN):

A static constructor cannot be called directly.

无法直接调用静态构造函数。

#5


2  

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?

静态构造函数不能包含任何参数。好吧,我认为它理论上可以 - 但是没有类的实例,所以它没有任何意义。如果你有这些参数,你会怎么做?调用其他静态方法?

#6


0  

  • Static Constructor is called automatically before first instance of the class is created.
  • 在创建类的第一个实例之前,会自动调用静态构造函数。
  • Declared by prefixing a static keyword to the constructor definition.
  • 通过在构造函数定义前添加static关键字来声明。
  • It can not not take access modifiers or have any parameters.
  • 它不能不接受访问修饰符或具有任何参数。

#7


0  

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");

#8


0  

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.

所以,我们必须使用参数少的静态构造函数。

#9


-1  

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!!

我根据上面的用户3567816的消息想出了这一点,虽然简洁而且有0票,但它是迄今为止最不优雅的解决方案并且非常独特。没有人对这类问题提出这样的建议。在嵌套类的建造者中没有更多但是更加冗长的形式参数!这绝对是辉煌的!!

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

我忍不住使用静态变量名Me来给VB.Net转。傻笑。

#1


24  

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.

正如MSDN所说,在创建第一个实例之前,会自动调用静态构造函数来初始化类。因此您无法发送任何参数。

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

如果CLR必须调用静态构造函数,它将如何知道传递它的参数?

#2


8  

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?

静态构造函数作为类型初始化的一部分自动调用。它们没有被明确调用...所以你无处可提供任何与构造函数参数相对应的参数。如果您永远不能为参数指定任何值,为什么还要允许参数?

#3


8  

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.

从理论上讲,这样的语法本来是可以设计和实现的,但是那时需要直接调用,因为现在一个简单的类引用不会知道要传递给它的参数。静态构造函数的要点是在使用类型之前执行类型级初始化。这样做会自动确保这种情况,而直接调用会留下足够的错误空间。

#4


4  

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

因为您无法直接调用它(根据MSDN):

A static constructor cannot be called directly.

无法直接调用静态构造函数。

#5


2  

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?

静态构造函数不能包含任何参数。好吧,我认为它理论上可以 - 但是没有类的实例,所以它没有任何意义。如果你有这些参数,你会怎么做?调用其他静态方法?

#6


0  

  • Static Constructor is called automatically before first instance of the class is created.
  • 在创建类的第一个实例之前,会自动调用静态构造函数。
  • Declared by prefixing a static keyword to the constructor definition.
  • 通过在构造函数定义前添加static关键字来声明。
  • It can not not take access modifiers or have any parameters.
  • 它不能不接受访问修饰符或具有任何参数。

#7


0  

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");

#8


0  

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.

所以,我们必须使用参数少的静态构造函数。

#9


-1  

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!!

我根据上面的用户3567816的消息想出了这一点,虽然简洁而且有0票,但它是迄今为止最不优雅的解决方案并且非常独特。没有人对这类问题提出这样的建议。在嵌套类的建造者中没有更多但是更加冗长的形式参数!这绝对是辉煌的!!

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

我忍不住使用静态变量名Me来给VB.Net转。傻笑。