Cached property vs Lazy

后端 未结 7 1340

In .NET 4 the following snippet with a cached property can also be written using the System.Lazy class. I measured the performance of both approaches and it\'s pret

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 13:38

    Lazy has some syncronization overhead to provide thread-safety whereas cached property is initiliazed by CLR way before any other code and you do not need to pay synronization cost

    From a testability point of view, Lazy is well tested and proven artifact.

    However, it has a very slight overhead, in my opinion, over other option

提交回复
热议问题