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

发布于 2024-11-25 12:04:37 字数 180 浏览 3 评论 0原文

根据 MSDN:

静态构造函数不采用访问修饰符或参数。

在创建第一个实例或引用任何静态成员之前,会自动调用静态构造函数来初始化类。

静态构造函数不能直接调用。

谁能解释一下为什么静态构造函数不能有参数?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

愚人国度 2024-12-02 12:04:37

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

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

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?

很糊涂小朋友 2024-12-02 12:04:37

静态构造函数作为类型初始化的一部分被自动调用。它们没有被显式调用...因此您无法提供与构造函数参数相对应的任何参数。如果您永远无法为参数指定任何值,为什么要允许参数?

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?

苏大泽ㄣ 2024-12-02 12:04:37

假设第一次引用该类时运行时会自动调用该构造函数,并且不能直接调用它,您将如何控制传递给此类构造函数的参数?

理论上,可以设计和实现这样的语法,但是这就需要直接调用它,因为现在简单的类引用不知道要传递什么作为参数给它。静态构造函数的全部要点是在使用类型之前执行类型级初始化。这样做会自动确保情况确实如此,而直接调用则会留下很大的出错空间。

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.

半夏半凉 2024-12-02 12:04:37

因为你不能直接调用它(根据 MSDN):

静态构造函数不能直接调用。

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

A static constructor cannot be called directly.

往事风中埋 2024-12-02 12:04:37

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

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?

江南月 2024-12-02 12:04:37
  • 静态构造函数在第一个实例之前自动调用
    该类已创建。
  • 通过在构造函数定义前添加 static 关键字来声明。
  • 它不能不采用访问修饰符或具有任何参数。
  • 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.
枯叶蝶 2024-12-02 12:04:37

为静态类创建一个空构造函数,并将参数化代码放入普通函数中。如果调用此函数,将创建静态类。

静态类:

static class DataB
{
    static DataB(){}

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

您可以这样创建它:

DataB.funcWithParams("some string");

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");
初心未许 2024-12-02 12:04:37

静态构造函数

因为静态构造函数会自动调用(我们无法控制静态构造函数的调用),所以我们无法将参数传递给静态构造函数。

如果我们不能将参数传递给静态构造函数,那么为什么我们将创建静态构造函数作为参数化。

因此,我们必须有无参数静态构造函数。

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.

死开点丶别碍眼 2024-12-02 12:04:37

静态构造函数由 CLR 自动隐式调用,它是在类下运行的第一个代码块。
我们无法将任何参数传递给静态构造函数,因为它们是隐式调用的,并且为了传递参数,我们必须显式调用它,这是不可能的。

更多说明请参阅此-在此处输入链接描述

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

梦旅人picnic 2024-12-02 12:04:37

自动调用静态构造函数来初始化类
在创建第一个实例或任何静态成员之前
参考。

静态构造函数不能直接调用。

静态构造函数不能显式调用,因此不带任何参数。我们假设静态构造函数可以传递参数。现在,因为我们无法显式调用构造函数,这意味着需要传递的任何参数都必须在构造函数定义本身中给出。例如

class Sample
{
    int a;
    int b;
    static Sample(int a1=10, int b1=20)
    {
        this.a = a1;
        this.b = b1;
    }
}

,如果您想一想,这个定义没有相关性,因为这些值可以直接在构造函数内分配,如下所示。

class Sample
{
    int a;
    int b;
    static Sample()
    {
         this.a = 10;
         this.b = 20;
    }
}

从某种意义上说,CLR 的隐式调用机制是静态构造函数不能有参数的原因。隐式调用不允许我们将不同的值传递给构造函数,因此具有参数的目的就失效了。

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.

胡渣熟男 2024-12-02 12:04:37

下面是一个允许嵌套类访问表单控件而不将表单作为参数传递给嵌套类构造函数的方法的示例:

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.

我根据上面 user3567816 的消息弄清楚了这一点,尽管该消息很简洁并且有 0 票,但它永远不会less 是迄今为止最优雅的解决方案并且非常独特。没有其他人对此类问题提出建议。嵌套类的构造函数中不再有丑陋的冗余表单参数!这绝对是太棒了!

我忍不住使用静态变量名 Me 来对 VB.Net 进行一些改动。傻笑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文