using System;
namespace Chernobyl.Values
{
///
/// This class is used to delay the computation or creation of some value
/// until it is actually needed.
///
/// The type whose value is to be delayed until needed.
public class LazyValue : HeldValue, IContainer
{
///
/// Initializes a new instance of the class.
///
/// The func.
public LazyValue(Func func)
{
Func = func;
HasValue = false;
}
///
/// Returns the value in question. If the value has not been computed or
/// created, then it will be and that value will be returned. Otherwise,
/// the already computed/created value will be returned.
///
public new T Value
{
get
{
if (HasValue == false)
{
_value = Func();
HasValue = true;
}
return _value;
}
}
///
/// True if this instance's has already been
/// computed/created, false if otherwise.
///
public bool HasValue { get; private set; }
///
/// The method that is to be invoked to generate the value.
///
Func Func { get; set; }
///
/// The backing field to .
///
T _value;
}
}