Skip to content

Instantly share code, notes, and snippets.

@normanlmfung
Last active March 30, 2024 03:12
Show Gist options
  • Save normanlmfung/3c960e19c829fa38617076ae7147b169 to your computer and use it in GitHub Desktop.
Save normanlmfung/3c960e19c829fa38617076ae7147b169 to your computer and use it in GitHub Desktop.
csharp_lazy
var lazyString = new Lazy<string>(
() =>
{
// Here you can do some complex processing
// and then return a value.
Console.WriteLine("Inside lazy loader");
return "Lazy loading!";
});
Console.WriteLine($"Is value created: {lazyString.IsValueCreated}"); // false
var lazyValue = lazyString.Value;
Console.WriteLine($"lazyValue: {lazyValue}");
Console.WriteLine($"lazyString.Value: {lazyString.Value}");
Console.WriteLine($"Is value created: {lazyString.IsValueCreated}"); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment