Friday, 27 September 2013

How to initialize structure with compiler generated backing fields

How to initialize structure with compiler generated backing fields

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; }
}

No comments:

Post a Comment