What is the internal reason that in C# structure cannot have an explicit default constructor?
Asked
Active
Viewed 492 times
1 Answers
1
From Using Constructors (C# Programming Guide)
Constructors for struct types resemble class constructors, but structs cannot contain an explicit default constructor because one is provided automatically by the compiler.
Chech this Why can't I define a default constructor for a struct in .NET?

Community
- 1
- 1

Soner Gönül
- 97,193
- 102
- 206
- 364
-
why it couldn't like in C++ - when explicit default constructor provided use it, when it is not provided generate and use provided by compiler? – Boris Pitel Jul 12 '13 at 11:23
-
@BorisPitel: The .NET assumes that a storage location of *any type* may be initialized to its default value by filling the memory it occupies with zeroes. This will set locations of reference types or `Nullable
` to `null`, will set numeric primitives to zero, and will set all fields of structures to the default values of their respective types. Such a design means that the system can initialize any type--no matter how complicated--simply by filling it with zero. Note also that the usefulness of being able to automatically call more complicated constructors... – supercat Jul 12 '13 at 15:15 -
...will in many cases hinge upon the ability to specify copy constructors and C++-style destructors; here again, .NET assumes that the only thing necessary to copy a struct is to copy the memory occupied thereby (which again merely requires knowing the size), and the only thing necessary to destroy a struct is to abandon or overwrite the space it occupies. Such assumptions greatly simplify the Framework and languages. While I will readily grant that proper struct constructors, copy constructors, and destructors are often useful, it's not clear that they're worth their substantial cost. – supercat Jul 12 '13 at 15:18