In C# 10 and later, why do default(T) and new T[n] skip a struct's parameterless constructor and its field initializers?
answer
- zeroed memory, no code runs
- constructors run on explicit new
- initializers live inside declared constructors
- CS8983 without any constructor
- zero must stay valid
basics
~20 sdefault(T) and array creation yield zero-filled memory without running any code. A struct's parameterless constructor, and the field initializers it executes, run only when that constructor is declared and the value is created with new S() or a generic new T().
solid answer
~40 sSince C# 10 a struct may declare a public parameterless constructor and field initializers, but both are ordinary constructor code. `default(S)`, the elements of `new S[n]` and a struct field inside a newly created class come from zero-filled memory, so no constructor runs and every field is 0, `false` or `null`. The constructor runs for an explicit `new S()` and for `new T()` under a `where T : struct` constraint. Field initializers run only inside an explicitly declared constructor: a struct with initializers and no constructor is rejected with CS8983, and when the struct declares only a constructor with parameters, `new S()` still gives the zeroed default with every initializer skipped. So the type must stay valid when all-zero, whatever its constructors do.
code
csharp · 25 linesvar made = new Vec3(); // the declared parameterless constructor runs
Vec3 zeroed = default; // all-zero bits, no constructor
var buffer = new Vec3[2]; // every element all-zero, no constructor
var generic = Create<Vec3>(); // new T() under a struct constraint runs it
Console.WriteLine($"new(): {made.Scale}, default: {zeroed.Scale}, array: {buffer[0].Scale}, new T(): {generic.Scale}");
var plain = new Pixel(); // Pixel declares no parameterless constructor
var shaded = new Pixel(3);
Console.WriteLine($"new Pixel(): {plain.Alpha}, new Pixel(3): {shaded.Alpha}");
static T Create<T>() where T : struct => new T();
struct Vec3
{
public float X { get; init; }
public float Scale { get; }
public Vec3() { Scale = 1f; }
}
struct Pixel
{
public byte Value { get; }
public byte Alpha { get; } = 255; // runs only inside a declared constructor
public Pixel(byte value) { Value = value; }
}go deeper
Recall that default and new arrays give zeroed structs, and that C# 10 allowed structs to declare a parameterless constructor.
Explain which expressions run the constructor and which yield zeros, and why field initializers need an explicitly declared constructor.
Trace a bug where an invariant set in a struct constructor or initializer is missing from array elements or default values, and redesign the type so zero is valid.
Set a rule that struct invariants must hold when zeroed, since constructors cannot cover arrays, defaults or containing objects.
## What C# 10 and C# 11 changed Before C# 10 a struct could not declare a parameterless constructor or initialize instance fields where it declared them; compiling such a struct with `LangVersion` 9 still reports CS8773, "Feature 'parameterless struct constructors' is not available in C# 9.0. Please use language version 10.0 or greater." **C# 10** lifted both limits, with conditions the compiler enforces: - A declared parameterless constructor must be `public`; anything else is **CS8958**, "The parameterless struct constructor must be 'public'." - A struct with **field initializers** must declare at least one constructor, with or without parameters; otherwise **CS8983**, "A 'struct' with field initializers must include an explicitly declared constructor." - Any explicitly declared constructor, with or without parameters, executes all of the struct's field initializers, as Microsoft's struct reference puts it. **C# 11** added *auto-default structs*: a field that a constructor leaves unassigned is set to its default value instead of causing a definite-assignment error. None of this changed how the runtime creates values without a constructor call. ## Paths that run no constructor at all The following produce the **all-zero bit pattern** directly: - `default(S)` and the `default` literal for a struct type; - every element of `new S[n]`, and every slot a growing `List<S>` allocates; - a field of type `S` inside a class or another struct when the containing object is created and nothing assigns that field; - `new S()` when `S` declares **no** parameterless constructor. The memory is zero-filled by the runtime, which is why an array of a million structs costs no constructor calls. Microsoft's struct reference says it directly: the default value expression ignores a parameterless constructor, and array instantiation produces elements with the default value. ## Paths that do run it - An explicit `new S()` when `S` declares a parameterless constructor. - `new T()` in a generic method constrained `where T : struct`; the compiled call reaches the declared constructor. - `Activator.CreateInstance<T>()`, which also ran it in a .NET 10 test. ## Field initializers belong to constructors | Struct declares | `new S()` | `new S(x)` | `default(S)` | |---|---|---|---| | no constructor, no initializers | zeroed | not available | zeroed | | initializers, no constructor | compile error CS8983 | compile error | compile error | | initializers and `S(int x)` only | **zeroed: initializers skipped** | initializers, then body | zeroed | | initializers and `public S()` | initializers, then body | not available unless declared | zeroed | The third row surprises people most: an initializer such as `Alpha = 255` looks like a fact about every `Pixel`, but `new Pixel()` with no parameterless constructor declared produces `Alpha` equal to 0. ## A worked example This runs on .NET 10. `Vec3` declares a parameterless constructor that sets `Scale` to 1; `Pixel` has an initializer but only a constructor with a parameter. ```csharp var made = new Vec3(); // Scale = 1 Vec3 zeroed = default; // Scale = 0 var buffer = new Vec3[2]; // Scale = 0 in every element var plain = new Pixel(); // Alpha = 0, initializer skipped var shaded = new Pixel(3); // Alpha = 255 Console.WriteLine($"{made.Scale} {zeroed.Scale} {buffer[0].Scale} {plain.Alpha} {shaded.Alpha}"); // 1 0 0 0 255 struct Vec3 { public float Scale { get; } public Vec3() { Scale = 1f; } } struct Pixel { public byte Alpha { get; } = 255; public Pixel(byte value) { } } ``` ## Design consequences 1. **Keep the all-zero state valid.** A parameterless constructor is a convenience for `new S()`, not a guarantee: arrays, `default` and fields of containing types will still produce zeros. The struct design guidelines ask for exactly this. 2. **Do not hide invariants in initializers.** If `Scale` must never be 0, either treat 0 as "unset" inside the members that read it or make the type a class, where `null` makes the missing construction visible. 3. **Expect zeros from collections.** A pooled buffer, a resized array or a struct field in a deserialized object all start zeroed. 4. **Prefer a factory or a constructor with parameters** for values that need setup, and document that `default` is the empty value. For the 3D vector in a game loop, a zeroed vector meaning "origin, scale 0" is easy to miss in a buffer of thousands: the safest design treats zero as a meaningful value rather than relying on the constructor.
- Does `new T()` in a generic method run a struct's parameterless constructor?Yes, under a `where T : struct` constraint the call creates the value through its parameterless constructor, so a declared one runs; in a .NET 10 test, `new T()` gave `Scale` equal to 1 for `Vec3`. `default(T)` in the same method still gives zeros, so generic code must not assume one or the other produced a constructed value.
- Why can a struct not simply require its parameterless constructor to run everywhere?Because value types are created in bulk by zero-filling memory: arrays, fields of containing objects and buffers. Running a constructor per element would make `new S[1_000_000]` call a million constructors and would change what `default` means. C# kept zero-filling as the creation path the runtime owns and made the parameterless constructor run only on explicit `new`.
- What does C# 11's auto-default struct feature change?Before C# 11 a struct constructor had to assign every field, or the compiler reported a definite-assignment error. Since C# 11 the compiler zeroes any field the constructor leaves unassigned. It removes boilerplate inside constructors; it does not make `default(S)` or arrays run them.
- Declaring a parameterless constructor guarantees every instance of the struct is initialized.
- Field initializers in a struct run whenever new S() is called.
- new S[10] calls the parameterless constructor ten times.
- Struct parameterless constructors can be private to force a factory method.
- default(S) calls the parameterless constructor if one is declared.