Hi,
Why the struct cannot have parameterless constructor in c#?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Suthish NairRank #92 · 22.8K reputationPosted Jun 29, 2011, 10:11 PM EDT
Because Struct are value types. The C# doesn't allows value types to have parameterless constructors, but CLR allows. Struct members are automatically initialized to their default values.
Om prakash Singh126 reputationPosted Jun 29, 2011, 10:30 PM EDT
Prasant JinagaRank #2,383 · 594 reputationPosted Jun 29, 2011, 10:47 PM EDT
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,
nullfor 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.
Thanks,
Prasant
VulpesRank #55 · 29.2K reputationPosted Jun 30, 2011, 1:23 AM EDT
This initializes all the struct's fields to their default values (zero for numeric types, false for bool, '\0' for char, null for string and other reference types). For example, this line:
int i = new int();
sets i equal to 0.