Lazy Loading in C#
Tags: csharp, programming, softwareengineering
Published: 2021-12-11 12:53:35

## Lazy loading
Lazy loading is a concept where we delay the loading of the object until the point where we need it. Putting in simple words, on demand object loading rather than loading objects unnecessarily.

## Lazy Initialization
Lazy initialization of an object means that its creation is deferred until it is first used. Primary use of lazy initialization is to

- Improve performance
- Avoid wasteful computation
- Reduce memory requirements

Imagine the following scenario. You have a customer object in memory that has an Orders property that contains an array of Order objects. If a user never asks to display the Orders or use the data in it, then there is no particular reason to use system memory to create it. To avoid wasting system resources when the object is not used you can declare Orders object for lazy initialization.

## Thread-Safe Initialization
By default Lazy<T> objects are thread-safe. In multi-threaded scenarios the first thread to access the Value property of a thread-safe Lazy<T> object initializes it for all subsequent access on all thread and all threads share the same data.

## Lazy Initialization example
To define a lazy initialized type use Lazy<T>. If no delegate is passed in the Lazy<T> constructor the wrapped type is created by using [Activator.CreateInstance](https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=net-6.0) when the value property is first accessed. If the type does not have a parameterless constructor, a run-time exception is thrown. In the following example we are going to create a Customer object containing an Orders property using Lazy initialization.

```csharp
public class Customer
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    private readonly Lazy<Orders> Orders;

    public Customer()
    {
        Orders = new Lazy<Orders>(GetMockOrders);
    }
        
    public Orders CustomerOrders => Orders?.Value;

    // Setup Orders data
    private static Orders GetMockOrders(){}

    // Setup Order list data
    private static IEnumerable<Order> GetMockOrder(){}
}
```
We are going to run the above in a console app and add a break point on line 12 before assign value on the `orders` variable.
![Debugger before lazy loading](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czcu9hfhids5oyn3wjiw.png)
If we now look at the Debug Output we can observe that `orderList` in `customerOrders` contains no Value. That is because of lazy loading `orderList` not yet been called. 
![Debugger after lazy loading](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo5awsdg9w57obvww3js.png) 
If we step over the break point and continue we can see some interesting things. 
![Debugger changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfago49n806dkl4mofkh.png)
First of all the Value of `orderList` is now containing a list of orders and expanding that list we can see the populated properties of that value. Second we can see that now the `isValueCreated` is equal to true, this means that the instance has been created.

To sum things up, lazy loading is a great optimization technique that can improve performance of our application and save memory resources.

You can download the code from [GitHub](https://github.com/TheodorosKarropoulos/PlayGround/blob/lazy_loading/src/PlayGround.Core/LazyLoading.cs)