What are the differences between LazyInitializer and Lazy<T> classes?
I know they both will initialize the object only on demand.
When do I need to use each of them?
What are the differences between LazyInitializer and Lazy<T> classes? I know they both will
Share
Lazy<T>(MSDN) is a generic wrapper which allows creating an instance ofTon demand by holding aTfactory method (Func<T>) and calling it whenValueproperty getter is accessed.LazyInitializer– static class with a set of static methods, this is just a helper which uses Activator.CreateInstance() (reflection) able to instantiate a given type instance. It does not keep any local private fields and does not expose any properties, so no memory usage overheads.Worth noting that both classes uses
Func<T>as instance factory.MSDN says in few words about
LazyInitializerclass:PS:
I found interesting a way how
LazyIntiializerchecks whether instance already initialized, it just compare a passed in reference to adefault(T), nice:What seems strange to me, it creates a new instance each time before an actual check: