.NET 4.0 has a nice utility class called System.Lazy that does lazy object initialization. I would like to use this class for a 3.5 project. One time I saw an implementation
If you don't need thread-safety, it's pretty easy to put one together with a factory method. I use one very similar to the following:
public class Lazy
{
private readonly Func initializer;
private bool isValueCreated;
private T value;
public Lazy(Func initializer)
{
if (initializer == null)
throw new ArgumentNullException("initializer");
this.initializer = initializer;
}
public bool IsValueCreated
{
get { return isValueCreated; }
}
public T Value
{
get
{
if (!isValueCreated)
{
value = initializer();
isValueCreated = true;
}
return value;
}
}
}