Created
July 27, 2009 14:56
-
-
Save davidpadbury/156436 to your computer and use it in GitHub Desktop.
This file contains 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
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