Author - StudySection Post Views - 13,766 views
Lazy Loading

Lazy Loading in C#

Lazy Loading is a technique that delays the initialization of an object. This is a new feature of C# 4.0. The basic idea of lazy loading is to load objects or data only when they are needed. A lazy loading pattern is also called Object on Demand. In other words, initialization of the object happens only on demand.

When to use Lazy loading?

It can be used when we are working with large objects when it is not in use. It is essential when the cost of object creation is very high and the use of the object is very rare.

Let us understand this through a real-time scenario:

Suppose we want to create an object of Customer class and the Customer class has a list of orders. So, when a new instance of Customer class is created, the order list will also be loaded at the same time. If in case, the order list is long, that will take a longer execution time and we may not need those data while fetching other Customer class property. Here, we can use the concept of lazy loading to load the data only when it is required.
code01
In the above example, the order object is also loaded even if it is not required.

To implement lazy loading, remove the loading of the Order object from the constructor, and in the Order get property, load the Order object only if it is not loaded as shown below:

code02

Now let us suppose, our Customer class is consumed by client code and if we run our client code then our Orders object is null i.e not loaded but as soon as the control reaches foreach loop, it will create the Order object collection.

In .Net, we have the Lazy<T> class which provides automatic support for lazy loading. We can modify our code using the Lazy Generic class by following the below steps.

  1. Create the object of orders using the Lazy generic class
    code03
  2. Attach this Lazy <> object with the method which will load the order’s data.
    code04

Now as soon as any client makes a call to the _Orders object, it will call the LoadOrders function to load the data as shown in the below code:
code05

Advantages of Lazy loading:

  • It helps to develop applications with high performance.
  • It also saves resources by delaying the initialization of expensive objects until they are needed.
  • It avoids unnecessary computation and memory consumption.
  • It minimizes the start up time of the application.
  • It avoids unnecessary database SQL execution.

Disadvantages of Lazy loading: The only disadvantage is that the code becomes quite complicated as we need to check whether loading of those high cost objects is needed or not due to which there is a slight decrease in performance.

If you need to prove your skills in the .NET framework, get .NET certified on StudySection. StudySection provides .NET certification exam for beginners as well as experts in the .NET framework. This .NET certification can improve your resume’s success rate.

Leave a Reply

Your email address will not be published.