Skip to content

Instantly share code, notes, and snippets.

@msarchet
Created January 22, 2014 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msarchet/8569530 to your computer and use it in GitHub Desktop.
Save msarchet/8569530 to your computer and use it in GitHub Desktop.
using System;
namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
// Annoying to have to check Value all the time
Console.WriteLine(Person.LazyPerson.Value == Person.LazyPerson.Value);
// Is there any gotcha to doing this?
Console.WriteLine(Person.Instance == Person.Instance);
Console.ReadKey();
}
}
public class Person
{
public static readonly Person Instance = new Lazier<Person>(() => new Person());
public static readonly Lazy<Person> LazyPerson = new Lazy<Person>(() => new Person());
}
public class Lazier<T> : Lazy<T>
{
public Lazier(Func<T> valueFactory): base(valueFactory) {}
public static implicit operator T(Lazier<T> obj)
{
return obj.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment