From .NET 4 onwards, Lazy<T>
can be used to lazily initialize objects. Intuitively, lazy initialization can also be performed in a public property's getter to provide the same functionality to the caller. I wonder if Lazy<T>
offers any inherent advantage over the latter and should thus be preferred?
Personally, I feel that Lazy<>
can quickly reduce code readability, but perhaps I've just seen it misused. On a plus side, it ensures thread safety, but there are many .NET synchronization constructs that - perhaps I'm wrong - make it quite easy to achieve the same inside a getter.
What are some considerations to be aware of when choosing the best approach?
Lazy<T>
. It's not so easy as you seem to think. – Natascha