加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么struct不能有无参数的构造函数

发布时间:2020-12-15 06:43:45 所属栏目:百科 来源:网络整理
导读:为什么struct不能有无参数的构造函数? 为CLR做什么或为什么不允许这样做的问题是什么? 请解释一下,因为我不明白. 解决方法 我不能有一个显式的无参数构造函数,只有隐式的构造函数才能将所有成员初始化为默认值. Although the CLR allows it,C# does not al
为什么struct不能有无参数的构造函数?
为CLR做什么或为什么不允许这样做的问题是什么?
请解释一下,因为我不明白.

解决方法

我不能有一个显式的无参数构造函数,只有隐式的构造函数才能将所有成员初始化为默认值.

Although the CLR allows it,C# does not allow structs to have a default parameterless 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,…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读