Created
January 22, 2014 23:21
-
-
Save msarchet/8569530 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
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