Cached property vs Lazy

后端 未结 7 1303

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:41

    Well if your performance is about the same then the only reason to use Lazy over the cached version would be if you aren't sure if the user is actually going to load the property.

    The point of Lazy is to wait until the user needs the resource and then create it at that instance in time. If they are always going to need to resource then there is no point in using Lazy, unless you need some of it's other purposes such as it being thread safe.

提交回复
热议问题