Support for Lazy Initialization in .Net 4.0

The .NET 4.0 framework is shipped with many new features, considering all aspects of development. To increase the performance and reduce memory consumption a new feature was introduced known as "Lazy Initialization". With earlier versions of .NET it could be implemented using some custom class but now you don't need to worry about it, just focus on the business logic, the framework will take care of your objects. Lazy initialization or lazy loading is the idea that the object will not be constructed, created, initialized, or loaded until it is absolutely needed. In this post, we'll see the different ways to use the Lazy feature.
 
Lazy initialization can be seen in designing the singleton pattern where we can have a static readonly property in a nested class that initializes the singleton object in a Lazy way.
  1. /// <summary>  
  2. /// Thread safe Singleton generic for earlier versions  
  3. /// </summary>  
  4. public class Singleton <T> where T:new()  
  5. {  
  6.     private Singleton() {}  
  7.    
  8.     public static T Instance  
  9.     {  
  10.         get { return SingletonCreator._instance; }  
  11.     }  
  12.    
  13.     class SingletonCreator  
  14.     {  
  15.         static SingletonCreator() { }  
  16.    
  17.         internal static readonly T _instance = new T();  
  18.     }      
Lazy<T> wrapper introduced to provide the support for Lazy initialization with its several overloads. The Lazy<T> may or may not ensures thread-safe initialization as it considers the performance and if required removes the thread-safe environment with locking & synchronization that can result in little effect on performance. But if you want to ensure that initialization should be thread-safe then you can use its overloads.
 
There are six overloads of Lazy<T>:
 

Lazy<T>()

 
Initializes a new instance of the Lazy<T> class. When lazy initialization occurs, the default constructor of the target type is used.
 
e.g.
  1. Lazy<Product> _product = new Lazy<Product>(); 

Lazy<T>(Boolean isThreadSafe)

 
When lazy initialization occurs, the default constructor of the target type and the specified initialization mode is used. i.e. considering the isThreadSafe.
 
e.g.
  1. Lazy<Product> _product = new Lazy<Product>(true); 

Lazy<T>(Func<T>)

 
When lazy initialization occurs, the specified initialization function is used. Means you can pass the parameterized constructor to the Lazy<T>
 
e.g.
  1. //Initializing the category class using parametric constructor  
  2. Lazy<Category> _category = new Lazy<Category>(() => new Category(23)); 

Lazy<T>(Func<T>, Boolean)

 
Initializes a new instance of the Lazy<T> class. When lazy initialization occurs, the specified initialization function and initialization mode are used by providing the value to the isThreadSafe argument.
 
e.g.
  1. //Initializing the category class using parametric constructor with parameter isThreadSafe  
  2. Lazy<Category> _category = new Lazy<Category>(  
  3.     () => new Category(23), true); 

Lazy<T>(LazyThreadSafetyMode)

 
Initializes a new instance of the Lazy<T> class that uses the default constructor of T and the specified thread-safety mode. The LazyThreadSafetlyMode is the enumeration provided in System.Threading namespace. It can have the below values.
 
e.g.,
  1. Lazy<Product> _product = new Lazy<Product>( System.Threading.LazyThreadSafetyMode.PublicationOnly); 
[For more details about LazyThreadSafetyMode enum visit the below link:
 

Lazy<T>(Func<T>, LazyThreadSafetyMode)

 
Initializes a new instance of the Lazy<T> class that uses the specified initialization function and thread-safety mode. Similar to the above initialization you can pass the special type of thread-safe mode with a generic delegate.
 
e.g.
  1. Lazy<Category> _category = new Lazy<Category>(  
  2.             () => new Category(23), System.Threading.LazyThreadSafetyMode.PublicationOnly); 
The Lazy<T> class may or may not be threadsafe but to ensure the thread-safe Initialization there's an alternative of Lazy<T>
 

Alternative to LazyInitialization System.Threading.LazyInitializer

 
This is a static implementation of the Lazy initializer which eliminates the overhead of the creation of Lazy objects. The methods of LazyInitializer are thread-safe and may be called from multiple threads concurrently. The routines of LazyInitializer avoid needing to allocate a dedicated, lazy-initialization instance, instead of using references to ensure targets have been initialized as they are accessed.
 
for e.g.
  1. ExpensiveData _data = null;  
  2. bool _dataInitialized = false;  
  3. object _dataLock = new object();  
  4. ThreadLocal<ExpensiveData> _expData = new ThreadLocal<ExpensiveData>(); 
Another alternative of LazyInitialization System.Threading.ThreadLocal
 
It is the same as Lazy<t>, but the only difference is that it stores data on Thread Local basis. So the values on each Thread would be a different copy of the Initialized object.
 
e.g.
  1. ThreadLocal<ExpensiveData> _expData = new ThreadLocal<ExpensiveData>(); 
You can also pass a generic delegate to pass values for a parametric constructor.
  1. ThreadLocal<ExpensiveData> _expData = new ThreadLocal<ExpensiveData>(() =>new ExpensiveData(objectState)); 
I hope this post helped you somehow understanding all support provided in .NET 4.0. So it's now up to you which approach to use and where to implement the Lazy behavior in your program or application.


Similar Articles