Constructors are used to construct the member variables of a class. As soon as the instance of the class is created the member variables of the class are initialized. This process seems fine if we have member variables which don't take huge memory. But suppose if we have a memory consuming resource and if we are initializing it as member variable, it can be prove to be memory hungary process.
To prevent from initializing the member variable as soon as the instance of class is constructed we can define member variable as property which can be initialized on demand(Lazy initialization). Let's check it through code snippet.
- public class ResourceHungry
- {
- //Class which takes lots of memory if initilized
- }
- public class ResourceConsumer
- {
- ResourceHungry _hungry;
- //Lazy Initialization
- public ResourceHungry Hungry
- {
- if(_hungry == null)
- hungry = new ResourceHungry();
- return _hungry;
- }
- }
To make it thread safe we need to have a lock outside if block as shown in the following code snippet.
- public class ResourceConsumer
- {
- ResourceHungry _hungry;
- readonly object _resourceHungryLock = new object();
- //Lazy Initialization
- public ResourceHungry Hungry
- {
- lock(_resourceHungryLock )
- {
- if(_hungry == null)
- hungry = new ResourceHungry();
- return _hungry;
- }
- }
- }
Starting framework 4.0, Lazy<T> class can be used to help with lazy initialization. If instantiated with an argument of true, it implements a thread-safe initialization pattern just described.
To use the Lazy<T>, we need to instantiate the class with the delegate that tells it how to instantiate a new value, and the argument true. We need to access its value via the Value property:
- Lazy<ResourceHungry> _resourceHungry= new Lazy<ResourceHungry>(() => new ResourceHungry(), true);
- public ResourceHungry ResourceHungry
- {
- get
- {
- return _resourceHungry.Value;
- }
- }

Join the conversation! Your thoughts help the community grow.