What is the internal reason that in C# structure cannot have an explicit default constructor?
1 Answer
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?
3 Comments
Boris Pitel
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?
supercat
@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<T> 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
...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.