Implementation of Lazy for .NET 3.5

后端 未结 4 1561
生来不讨喜
生来不讨喜 2020-12-04 16:54

.NET 4.0 has a nice utility class called System.Lazy that does lazy object initialization. I would like to use this class for a 3.5 project. One time I saw an implementation

4条回答
  •  自闭症患者
    2020-12-04 17:45

    Some funny (but not very usable) stuff can be added: implicit coversion from delegate:

    public static implicit operator Lazy(Func initializer)
    {
        return new Lazy(initializer);
    }  
    

    And usage

    private static Lazy Value = new Func(() => 24 * 22);
    

    C# compiler have some problem with performing this conversion, for example assigning lambda expression does not work, but it is one more thing causes your colleguas to think a bit :)

提交回复
热议问题