Brief Description about on Lazy Object Initialization in C#

In C# .NET we have so many keywords and having a chance to get more keywords. So in this article we will talk about LAZY keyword. I hope following demonstration covers about lazy keyword and exact usage and its implementation.

Lazy:
Give some lazy performance to your application. But it is useful to give more functions to your properties and objects.

properties

Conceptual view

How to change Constructor behavior with LAZY keyword?

In general, Constructor will invoke automatically when object define.

Program

  1. class Program  
  2. {  
  3.     public Program()  
  4.     {  
  5.         Console.WriteLine("Constructor");  
  6.     }  
  7.     static void Main(string[] args)  
  8.     {  
  9.         Program objProgram = new Program();  
  10.         Program objProgram2 = new Program();  
  11.         Console.Read();  
  12.     }  
  13. }  
  14. Let’ s see what happen with LAZY  
  15. Call your class constructor with lazy value.So you can restrict your constructor calling!static void Main(string[] args)  
  16. {  
  17.     Program objProgram = new Program();  
  18.     Program objProgram2 = new Program();  
  19.     Lazy < Program > objlazyProgram = new Lazy < Program > ();  
  20.     Console.WriteLine("=========");  
  21.     var GetValuefromConstructor = objlazyProgram.Value;  
  22.     Lazy < Program > objlazyProgram2 = new Lazy < Program > ();  
  23.     Console.Read();  
  24. }  
Console


Next Recommended Reading Lazy initialization in C#