Skip to content

Struct Parameterless Constructors

Question

Can you override the default, parameterless constructor of a value type?

Short interview answer

A struct can declare a public parameterless instance constructor, but default(T) and array initialization still produce the zero-initialized default value and do not call it.

Detailed answer

Modern C# permits an explicitly declared public parameterless struct constructor, but it does not replace the language-defined default value:

var created = new Temperature();  // Calls the declared constructor.
var defaulted = default(Temperature); // Zero-initialized; does not call it.

new Temperature[10] also contains zero-initialized values. Code must therefore remain correct for the default state.

Sources