A normal ‘getter’, like;
/// <summary>
/// Gets the image.
/// </summary>
/// <value>The image.</value>
public override Image Image
{
get
{
return Common.Icons.Get("StraightElevator");
}
}
or the newer ‘Expression Bodied Property’ style
public override Image Image => Common.Icons.Get("StraightElevator");
will call
Common.Icons.Get("StraightElevator")
and return the image reference every time it is called.
But did we really intend this ?
In above example we know from our domain knowledge that the image resource should probably have been a const or readonly reference since the image is loaded from disk and never changed during runtime.
So why;
A better solution would be to fetch the image once and store it in a readonly field.
But instead of doing the whole cermony of; declaring a readonly field, initializing it in the contructor and create a getter – you can simpy use the ‘Auto-Property Initializer’ (C#6.0 syntax feature), like so;
/// <summary>
/// Gets the image.
/// </summary>
/// <value>The image.</value>
public override Image Image { get; } = Common.Icons.Get("StraightElevator");
This creates a readonly field, initializes it once and gives you a public property all in one line of code. And used in the right scenarios it helps performance, while at the same time minimizes SLOC count – a win-win ?.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |