Created
November 23, 2011 13:34
-
-
Save jfromaniello/1388675 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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