Ultra simple question... How does one properly initialize private backing fields on a structure? I get a compiler error:
Backing field for automatically implemented property 'Rectangle.Y' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.
Heres the source code.
public struct Rectangle
{
public Rectangle(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
public int Width { get; private set; }
public int Height { get; private set; }
public int X { get; private set; }
public int Y { get; private set; }
}
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...