Disadvantages of Lazy?

后端 未结 7 1828
青春惊慌失措
青春惊慌失措 2021-01-31 14:37

I recently started using Lazy throughout my application, and I was wondering if there are any obvious negative aspects that I need to take into account when using Lazy<

相关标签:
7条回答
  • 2021-01-31 15:15

    What exactly do you mean with "throughout my application"?

    I think it should only be used when you're not sure if the value will be used or not, which may only be the case with optional parameters that take a long time to compute. This could include complex calculations, file-handling, Webservices, database access and so on.

    On the other hand, why use Lazy here? In most cases you can simply call a method instead of lazy.Value and it makes no difference anyway. BUT it's more simple and obvious to the programmer what's happening in this situation without Lazy.

    One obvious upside may be already implemented caching of the value, but I don't think this is such a big advantage.

    0 讨论(0)
提交回复
热议问题