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.
Conceptual view
How to change Constructor behavior with LAZY keyword?
In general, Constructor will invoke automatically when object define.
- class Program
- {
- public Program()
- {
- Console.WriteLine("Constructor");
- }
- static void Main(string[] args)
- {
- Program objProgram = new Program();
- Program objProgram2 = new Program();
- Console.Read();
- }
- }
- Let’ s see what happen with LAZY
- Call your class constructor with lazy value.So you can restrict your constructor calling!static void Main(string[] args)
- {
- Program objProgram = new Program();
- Program objProgram2 = new Program();
- Lazy < Program > objlazyProgram = new Lazy < Program > ();
- Console.WriteLine("=========");
- var GetValuefromConstructor = objlazyProgram.Value;
- Lazy < Program > objlazyProgram2 = new Lazy < Program > ();
- Console.Read();
- }