I have read a bit about lazy loading in c# and this might seem like a very basic question, but I am wondering whether autoproperties are lazy loading per default. Eg.:
public Color MyColor { get; set; }
Or would I have to implement something along the lines of
private Color _color;
public Color MyColor
{
get
{
if(_color==null)
{
_color=new Color("red");
}
return _color;
}
}
Thanks
Thomas
Auto properties simply get a backing field generated by the compiler.
So, this:
Will end up like this:
So they are not lazy loaded – you will need to implement this yourself.
You could instantiate the backing field like this:
With auto properties, you could set a default using the constructor: