Lazy initialization
For improving the performace of the application object will initializaed at first usage. This process is called as Lazy initialization.Example:
In the singleton design pattern we are using this Lazy initialization. Please check below example. In this we are using lazy Initialization in public static SingletonTest returnInstanc() this method.
class Program { static void Main(string[] args) { //can't use new keyword due to protection level. SingletonTest logFile = SingletonTest.returnInstanc(); //http://aspnettutorialonline.blogspot.com/ logFile.Code = "http://aspnettutorialonline.blogspot.com/"; Console.WriteLine(logFile.Code); Console.ReadLine(); } } //singleton class having private constructor. class SingletonTest { private static SingletonTest singletonTest = null; //http://aspnettutorialonline.blogspot.com/ //creating a property private string code = string.Empty; private SingletonTest() { code = string.Empty; } public static SingletonTest returnInstanc() { //lazy initialization if (singletonTest == null) singletonTest = new SingletonTest(); return singletonTest; } //exposing property to the other classes. public string Code { get { return code; } set { code = value; } } }
If you have any queries or suggestions, please fell free to ask in comments section.
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.