Skip to content

Instantly share code, notes, and snippets.

@davidpadbury
Created July 27, 2009 14:56
Show Gist options
  • Save davidpadbury/156436 to your computer and use it in GitHub Desktop.
Save davidpadbury/156436 to your computer and use it in GitHub Desktop.
void Main()
{
var name = new LazyValue<string>(LoadName); // Technically you don't even need the generic specification here but I'm being verbose
Console.WriteLine(name.Value);
}
private static string LoadName()
{
return "Pete Smith";
}
public class LazyValue<T>
{
private readonly Func<T> loadFunction;
private bool loaded;
private T loadedValue;
public LazyValue(Func<T> loadFunction)
{
this.loadFunction = loadFunction;
}
public T Value
{
get
{
if (!loaded)
{
loadedValue = loadFunction();
loaded = true;
}
return loadedValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment