Caching Intensive Calculation in Property Getter

Just keep a flag to remember if the calculation have been done.

public class SomeClass
{
    // ... constructor and other stuff

   private bool _propertyCalculated;
   private int _someProperty;

    public int SomeProperty
    {
        get
        {
            if (!_propertyCaculated)
            {
                _someProperty = SomeHeayCalculation();
                _propertyCaculated = true;
            }
            return _someProperty;
        }
    } 
}

Other than what Jon suggested, you could use this pattern:

public class SomeClass
{
    // ... constructor and other stuff
    private Foo _foo;

    public Foo SomeProperty
    {
        get
        {
            return _foo ?? (_foo = SomeHeayCalculation());
        }
    } 
}

It's worth noting that this really breaks down (read: becomes less readable) for value types, unless you want to wrap them in Nullable<T>. In that's your case, you may want to stick with Lazy<T> if available.


Yup - Lazy<T>, assuming you're using .NET 4:

public class SomeClass
{
    private readonly Lazy<Foo> foo = new Lazy<Foo>(SomeHeayCalculation);
    // ... constructor and other stuff

    public Foo SomeProperty
    {
        get
        {
            return foo.Value;
        }
    } 
}

I assume you're trying to avoid performing the calculation if the property is never accessed. Otherwise, just perform it upfront on construction.

Note that properties are often understood to be "cheap" to evaluate - and while you're making this lazy so that later accesses are cheap, this is still potentially going to be "heavy" enough on the first access to make a property inappropriate. Consider a ComputeXyz method instead.


Just cache the calculation in a private variable like so:

public class SomeClass
{        
    // ... constructor and other stuff

    private int? calculation = null;

    public int SomeProperty
    {
        get
        {
            if (!calculation.HasValue)
                calculation = SomeHeayCalculation();

            return calculation.Value;
        }
    } 
}

Tags:

C#