.NET 4.0 has a nice utility class called System.Lazy that does lazy object initialization. I would like to use this class for a 3.5 project. One time I saw an implementation somewhere in a stackoverflow answer but I can't find it anymore. Does someone have an alternative implementation of Lazy? It doesn't need all the thread safety features of the framework 4.0 version.
Updated:
Answers contain a non thread safe and a thread safe version.
lock
a private object than tolock (this)
, since you can't control who else might lock on yourLazy
instance. Second, I don't think makingisValueCreated
avolatile
field serves any purpose when you're already using a critical section (does it? Correct me if I'm wrong). – Ardisardisj