为什么结构不能缺少参数的构造函数? [英] why structures can not have parameter less constructors?

查看:132
本文介绍了为什么结构不能缺少参数的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何版本的c#,请回答上面的问题.

解决方案

尽管CLR允许,但C#不允许结构的默认参数少构造函数.原因是,对于值类型,默认情况下,编译器既不会生成默认构造函数,也不会生成对默认构造函数的调用.因此,即使您碰巧定义了默认构造函数,也不会调用它,这只会使您感到困惑.为避免此类问题,C#编译器不允许用户定义默认构造函数.而且因为它不会生成默认的构造函数,所以在定义字段时无法初始化它们.

Ref:-为什么结构构造函数必须至少具有一个参数 [ http://stackoverflow.com/questions/2372061/c-sharp-struct-no-parameterless-constructor-see-what-i-need-to-accomplish [ http://stackoverflow.com/Questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net [ Anders Hejlsberg 做出了决定吗?)

我并不是说这个决定不好.相当好,至少足够好.我只是说,我确信其他一些合理的决定是可能的,而且不可能说其中之一绝对是最好的.我对许多面向对象的语言(包括.NET和C#的前身Delphi;在Anders Hejlsberg是Borland的首席架构师时创建的体系结构)中的各种构造函数规则非常熟悉,我可以说出一些语法和语义规则不同,但不如.NET好,但是有些规则不同,至少不比.NET差.

鉴于此,在许多资料中找到的所有解释,包括Varun解释或引用的解释,都有些有趣.首先,它们看起来很合理.一些论点强调了所讨论规则的真正价值.唯一的问题是:他们不证明任何东西.它们只是说明了一个众所周知的现象:许多人非常熟练地将推理调整为已知的事实.



我没有证明任何事情,但我需要说明至少一个论点在逻辑上是不完善的.让我们选择一个:

Varun Sareen写道:

.NET公共语言运行时(CLR)不保证将调用无参数构造函数. .

首先,这句话是不正确的.除非定义了显式构造函数,否则类始终具有隐式无参数构造函数.显式构造函数在语义上等同于具有空主体的构造函数.这样,是否调用就在语义上并不重要,但是无论如何都应调用继承的构造函数的整个链.对于显式无参数构造函数,可以保证调用它.

这样,带引号的语句是不正确的,或者对于类构造函数没有意义.但这对无参数struct构造函数有意义吗?否,因为语法始终不允许此类构造函数.

这是循环证明"的示例.推测:语法不允许使用无参数构造函数,因为如果允许使用无参数构造函数,则将无法保证对它们的调用.但是,为什么要做出不保证无参数struct构造函数的调用"的决定?这纯粹是推测性的考虑.

[END EDIT]

在我所有这些作者的尊敬下,我真正理解了软件开发:这是解释迷信本质的现象.

—SA


please answer the question above for any version of c#.

解决方案

Although the CLR allows it, C# does not allow structs to have a default parameter less constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor. So, even if you happened to define a default constructor, it will not be called and that will only confuse you. To avoid such problems, the C# compiler disallows definition of a default constructor by the user. And because it doesn''t generate a default constructor, you can''t initialize fields when defining them.

Ref:- Why must struct constructors have at least one argument[^]

The .NET Common Language Runtime (CLR) does not guarantee that parameterless constructors will be called. If structs were permitted to have default, parameterless constructors, the implication would be that default constructors would always be called. Yet, the CLR makes no such guarantee.

For instance, an array of value types will be initialized to the initial values of its members—i.e., zero for number type primitive members, null for reference types, and so forth—and not to the values provided in a default constructor. This feature makes structs perform better; because, constructor code need not be called.

So, requiring that a constructor contain a minimum of one parameter reduces the possibility that a constructor will be defined which is expected to be called every time the struct type is built.


Helpful links:-

http://stackoverflow.com/questions/2372061/c-sharp-struct-no-parameterless-constructor-see-what-i-need-to-accomplish[^]

http://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net[^]


In my strong opinion, the only really valid answer will be: because creators of .NET decided to design it this way. (Did Anders Hejlsberg made a decision?)

Not that I say that the decision is not good. It''s quite good, at least good enough. I just say, that I am sure that some other reasonable decisions are possible, and it''s not possible to say that one of them is absolutely the best. I am well familiar with variety of constructor rules in a number of object-oriented languages (including .NET and C# predecessor, Delphi; with architecture created when Anders Hejlsberg was a chief architect in Borland), and I can tell that some syntax and semantic rules are different and not as good as in .NET, but some are different and at least not worse than that of .NET.

In view of this, all the explanations found in many source, including those explained or referenced by Varun, are somewhat funny. First, they look reasonable. Some arguments emphasize the real value of the rules under discussion. The only problem is: they do not proof anything; they just illustrate the well known phenomenon: many people are very skilled to adjust reasoning to the know fact.

[EDIT]

I did not proof anything, but I need to illustrate that at least one argument is logically imperfect. Let''s pick this one:

Varun Sareen wrote:

The .NET Common Language Runtime (CLR) does not guarantee that parameterless constructors will be called.

First, this statement is simply not correct. A class always have either implicit parameterless constructor unless an explicit constructor is defined. An explicit constructor is semantically equivalent to the constructor with empty body. In this way, it is semantically not important if it is called or not, but the whole chain of inherited constructors should be called anyway. As to the explicit parameterless constructor, it is guaranteed to be called.

In this way, the quoted statement is not true or makes no sense for class constructors. But does it make sense for parameterless struct constructors? No, because the syntax does not allow such constructors anyway.

Here is the example of circular "proof". The speculation goes: parameterless constructors are not allowed by the syntax, because if they were allowed, their call would not be guaranteed. But why the decision "not to guarantee a call the a parameterless struct constructors" would be made? This is a purely speculative consideration.

[END EDIT]

With all my respect to those authors really understanding software development: this is the phenomenon which explains the nature of superstition.

—SA


这篇关于为什么结构不能缺少参数的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆