Yup - Lazy<T> , assuming you are using .NET 4:
public class SomeClass { private readonly Lazy<Foo> foo = new Lazy<Foo>(SomeHeayCalculation);
I assume that you are trying to avoid doing the calculation if the property never accesses. Otherwise, just follow it when building.
Note that properties are often understood to be βcheapβ for pricing β and while you are doing it lazy so that later calls are cheap, it will still potentially be βheavyβ enough for the first access to make the property inappropriate. Instead, consider the ComputeXyz method.
source share