Why struct can not have parameterless constructor? What's the problem in doing this for CLR or why it's not allowed ? Please explain it as I don't understand it.
-
Duplicate: [Why can’t I define a default constructor for a struct in .NET](http://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net) – Marc Gravell Feb 22 '09 at 22:03
2 Answers
I cannot have an explicit parameterless constructor, only the implicit one, that initializes all members to their default.
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, ...
Quite a reasonable explanation can be found at: http://en.csharp-online.net/CSharp_FAQ:_Why_must_struct_constructors_have_at_least_one_argument
Quoting: "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."

- 8,040
- 7
- 41
- 52