This is a problem using automatically implemented properties in structs. You need to explicitly chain to the parameterless constructor:
public Rectangle(int x, int y, int width, int height) : this()
{
X = x;
Y = y;
Width = width;
Height = height;
}
The compiler doesn't "know" that setting each of the properties will set the relevant fields - it needs to see that all the fields (which you can't access explicitly because they're being autogenerated) are assigned. Fortunately the parameterless constructor does that for you.
Personally I'd just use real readonly fields and manual properties instead, mind you:
public struct Rectangle
{
private readonly int x, y, width, height;
public int X { get { return x; } }
public int Y { get { return y; } }
public int Width { get { return width; } }
public int Height{ get { return height; } }
public Rectangle(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
I wish C# had a way of declaring "really" readonly automatically implemented properties, such that the setters could only be called in the constructor and would be translated directly to writes to readonly fields, but never mind...