When should I use Lazy<T>?

danyolgiax picture danyolgiax · Jul 27, 2011 · Viewed 134.2k times · Source

I found this article about Lazy: Laziness in C# 4.0 – Lazy

What is the best practice to have best performance using Lazy objects? Can someone point me to a practical use in a real application? In other words, when should I use it?

Answer

James Michael Hare picture James Michael Hare · Jul 27, 2011

You typically use it when you want to instantiate something the first time its actually used. This delays the cost of creating it till if/when it's needed instead of always incurring the cost.

Usually this is preferable when the object may or may not be used and the cost of constructing it is non-trivial.