Lazy Loading and Eager Loading in Singleton Design Pattern C# | Thread-safe Design Pattern | Part-7

Опубликовано: 12 Октябрь 2024
на канале: Code with Salman
932
12

The Eager loading in the singleton design pattern is nothing but a process in which we need to initialize the singleton object at the time of application start-up rather than on-demand and keep it ready in memory to be used in the future. The advantage of using Eager Loading in the Singleton design pattern is that the CLR (Common Language Runtime) will take care of object initialization and thread safety. That means we will not require to write any code explicitly for handling the thread safety for a multithreaded environment.
The Lazy or Deferred Loading is a design pattern or you can say it’s a concept that is commonly used to delay the initialization of an object until the point at which it is needed. So the main objective of Lazy loading is to load the object on-demand or you can say object when needed. The most important point that you need to keep in mind is that, you need to use the Lazy loading when the cost of the object creation is very high as well as the use of that object is very rare. The lazy loading improves the performance of an application if it is used properly. We can use the Lazy keyword to make the singleton instance lazy loading.