I have a method that takes around 200ms to run and would therefor like to cache the result. This result will be used frequently, but it never changes.
I'm not entirely sure what the best solution would be for this, should I put this to a property or a get method?
As an example:
1
private string _result;
public string Result => _result ?? (_result = GetSlowResult());
2
private string _result;
public string GetResult() => _result ?? (_result = GetSlowResult());
Personally, from a property I typically expect it to be available "now" rather than "later" but with a Get method I expect the result to always retrieve a fresh GetSlowResult
rather than using a cached value. I could change the method name to GetCachedResult
but I'm not sold on that either as then it would seem you need to have called a GetResult
method first.
Are there any guidelines to this? What do you prefer?
Aucun commentaire:
Enregistrer un commentaire