为什么静态构造函数没有任何参数?
根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
正如 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?
静态构造函数作为类型初始化的一部分被自动调用。它们没有被显式调用...因此您无法提供与构造函数参数相对应的任何参数。如果您永远无法为参数指定任何值,为什么要允许参数?
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?
假设第一次引用该类时运行时会自动调用该构造函数,并且不能直接调用它,您将如何控制传递给此类构造函数的参数?
理论上,可以设计和实现这样的语法,但是这就需要直接调用它,因为现在简单的类引用不知道要传递什么作为参数给它。静态构造函数的全部要点是在使用类型之前执行类型级初始化。这样做会自动确保情况确实如此,而直接调用则会留下很大的出错空间。
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.
因为你不能直接调用它(根据 MSDN):
Because you can't call it directly (as per MSDN):
静态构造函数不能有任何参数。好吧,我想理论上可以 - 但没有该类的实例,所以它没有任何意义。如果你有这些参数,你会怎么处理它们?调用其他静态方法?
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?
该类已创建。
the class is created.
为静态类创建一个空构造函数,并将参数化代码放入普通函数中。如果调用此函数,将创建静态类。
静态类:
您可以这样创建它:
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:
you can create it like this:
静态构造函数
因为静态构造函数会自动调用(我们无法控制静态构造函数的调用),所以我们无法将参数传递给静态构造函数。
如果我们不能将参数传递给静态构造函数,那么为什么我们将创建静态构造函数作为参数化。
因此,我们必须有无参数静态构造函数。
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.
静态构造函数由 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
静态构造函数不能显式调用,因此不带任何参数。我们假设静态构造函数可以传递参数。现在,因为我们无法显式调用构造函数,这意味着需要传递的任何参数都必须在构造函数定义本身中给出。例如
,如果您想一想,这个定义没有相关性,因为这些值可以直接在构造函数内分配,如下所示。
从某种意义上说,CLR 的隐式调用机制是静态构造函数不能有参数的原因。隐式调用不允许我们将不同的值传递给构造函数,因此具有参数的目的就失效了。
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.
If you give it a thought, this definition has no relevance because the values could have been directly assigned inside the constructor as below.
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.
下面是一个允许嵌套类访问表单控件而不将表单作为参数传递给嵌套类构造函数的方法的示例:
我根据上面 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:
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.