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

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

概述为什么struct不能有无参数的构造函数? 为CLR做什么或为什么不允许这样做的问题是什么? 请解释一下,因为我不明白. 我不能有一个显式的无参数构造函数,只有隐式的构造函数才能将所有成员初始化为默认值. Although the CLR allows it, C# does not allow structs to have a default parameterless constructor 为什么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,…

总结

以上是内存溢出为你收集整理的c# – 为什么struct不能有无参数的构造函数全部内容,希望文章能够帮你解决c# – 为什么struct不能有无参数的构造函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1259448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存