C# Lazy Class Example

In this tutorial we will learn how to use c# lazy class, or how to perform lazy loading in c# application development, c# lazy class allow us to load any data on demand instead of loading at the time of initialization.

Lazy<List<int>> list3Lazy = data.GetList3();
List<int> list3 = list3Lazy.Value; 

c# lazy loading

What is lazy loading?

Lazy loading is basically loading data when its required, not at the time when the object instance is created, the lazy loading technique can help boosting application performance some extent if used correctly.

Think of real-time business scenario, when you want to create a client object along with client order list, now in general you probably will have order list property in client object, so when a new instance of client object is created, the order list also will be loaded at the same time. If the order list is long, that will take longer the execution time, when you may not need those data while accessing other client class property, which can be avoided by using lazy class, you can load the data only when the data is required.

How to perform lazy loading in c#

Now to experiment if c# lazy class really helps in performance improvement, we do the following exercise; create two properties with exact same amount of data. One simple list object and another one lazy list object.

public class data
{
    public static List<int> GetList1()
    {
        List<int> list1 = new List<int>();
        for (int i = 0; i <= 5000000; i++)
        {
                list1.Add(i);
        }
        return list1;
    }

    public static Lazy<List<int>> GetList3()
    {
        Lazy<List<int>> list3 = new Lazy<List<int>>();
        for (int i = 0; i <= 5000000; i++)
        {
            list3.Value.Add(i);
        }
        return list3;
    }
}

Now create two separate stop watch object to understand how much time is consumed for each execution.

public static void displayData()
{

    Stopwatch st = new Stopwatch();
    st.Start();
    dobj = new data();
    List<int> list1 = data.GetList1();
    foreach (int i in list1)
    {
        //just executing the loop to check time duration
    }
    st.Stop();
    Console.WriteLine("List Time:" + st.Elapsed);
    st.Reset();


    Stopwatch st1 = new Stopwatch();
    st1.Start();
    dobj = new data();
    Lazy<List<int>> list3Lazy = data.GetList3();
    List<int> list3 = list3Lazy.Value;
    foreach (int i in list3)
    {
        //just executing the loop to check time duration
    }
    st1.Stop();
    Console.WriteLine("Lazy List Time:"+ st1.Elapsed);
    st1.Reset();
}

You will be surprised to see the execution time; the lazy list took half time to perform than the normal list object.

C# Lazy Property Set Get

Let's assume you have Client class, and there is property called OrderItems, now while displaying client information we also want to keep the order details ready. now instead of creating list property we can create lazy list property Lazy<List<OrderItem>> like example below.

public class Client
{
    public Lazy<List<OrderItem>> OrderItems { get; set; }
}

To set value in lazy list property use new keyword.

List<OrderItem> items = dto.GetOrderList();

Client clientmodel = new Client();
clientmodel.OrderItems = new Lazy<List<OrderItem>>(items);

You may be interested in following tutorial

 
C# Lazy Example
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers
Lazy Loading in C#
.Net C# Examples | Join .Net C# Course