Prior to C# 11 struct constructors were required to explicitly assign every field and auto-property. This was tedious when only some fields needed non-default values and the rest could remain at their defaults.
C# 11 removes this requirement by having the compiler automatically initialize any fields not explicitly assigned by the constructor to their default values.
Code
C#
struct Point
{
public int X { get; set; }
public int Y { get; set; }
public bool IsValid { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
// IsValid is automatically initialized to default (false)
}
}C#
struct Point
{
public int X { get; set; }
public int Y { get; set; }
public bool IsValid { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
IsValid = default; // Required or the compiler would error
}
}