Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created November 23, 2011 13:34
Show Gist options
  • Save jfromaniello/1388675 to your computer and use it in GitHub Desktop.
Save jfromaniello/1388675 to your computer and use it in GitHub Desktop.
public class Lazy<T>
{
private Func<T> factory;
private object objectLock = new Object();
private T lazyValue;
public Lazy(Func<T> factory)
{
this.factory = factory;
}
public bool IsValueCreated { get; private set; }
public T Value
{
get
{
lock(objectLock)
{
if(!IsValueCreated)
{
lazyValue = factory();
IsValueCreated = true;
}
}
return lazyValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment