100  
csharp
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Nov 12, 2020

How to perform lazy initialization in C#

Автор:
Joydip Kanjilal
Источник:
Просмотров:
1006

Lazy initialization is a technique that defers the creation of an object until the first time it is needed. In other words, initialization of the object happens only on demand. Note that the terms lazy initialization and lazy instantiation mean the same thing — they can be used interchangeably. By taking advantage of lazy initialization, you can improve the application’s performance by avoiding unnecessary computation and memory consumption. In this article we’ll look at how we can perform lazy initialization in C#.

Let’s understand lazy loading with a simple example. Consider two classes, Customer and Order. The Customer class contains an Orders property that in turn references a collection of instances of the Order class. The Orders collection may contain a large amount of data and may even need a database connection to connect to the database and retrieve records. In such a case, there is no point in loading data in the Orders property until we need the data. Lazy initialization allows us to load the Orders collection only when the data is asked for.

Using the Lazy<T> class in C#

Although you can write your own custom code to implement lazy initialization, Microsoft recommends using the Lazy<T> class instead. The Lazy<T> class in the System namespace in C# was introduced as part of .Net Framework 4.0 to provide a thread-safe way to implement lazy initialization. You can take advantage of this class to defer the initialization of resource-intensive objects in your application.

When you use the Lazy<T> class, you need to specify the type of object you intend to create lazily in the type argument. Note that lazy initialization occurs when you access the Lazy<T>.Value property. Here is an example of how the Lazy<T> class can be used:

Lazy<IEnumerable<Order>> orders = new Lazy<IEnumerable<Order>>();
IEnumerable<Order> result = lazyOrders.Value;

Now, consider two classes, Author and Blog. An author can write many blog posts, so you have a one-to-many relationship between the Author and Blog classes as shown in the code snippet below.

public class Author
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public List<Blog> Blogs { get; set; }
}

public class Blog
{
    public int Id { get; set; }
    public string Title { get; set; }
    public DateTime PublicationDate { get; set; }
}

Note that the one-to-many relationship between the Author and Blog classes has been represented using a List property (of type Blog) in the Author class. Using this property, the Author class can hold a collection of one or more instances of the Blog class.

Now suppose we need to display only the details of an author (first name, last name, and address) in the user interface. There is no point in loading the blog details for the author in this case; we want to load the blog details lazily. Here is the updated Author class that addresses this need. Note the usage of the Lazy<T> class.

public class Author
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public Lazy<IList<Blog>> Blogs => new Lazy<IList<Blog>>(() => GetBlogDetailsForAuthor(this.Id));

    private IList<Blog> GetBlogDetailsForAuthor(int Id)
    {
        // Write code here to retrieve all blog details for an author.
    }
}

Using the generic Lazy class in C#

Let’s now look at how we can take advantage of a generic Lazy class to implement the Singleton design pattern. The following version of the StateManager class is thread-safe. At the same time, it demonstrates lazy initialization. Note that the explicit static constructor has been used to ensure that the C# compiler doesn’t mark the type as beforefieldinit.

public sealed class StateManager
{
    private StateManager()
    {
    }

    public static StateManager Instance
    {
        get
        {
            return Nested.obj;
        }
    }

    private class Nested
    {
        static Nested()
        {
        }
        internal static readonly StateManager obj = new StateManager();
    }
}

Here is a lazy implementation of the StateManager class that leverages the Lazy<T> class. You can see how the Lazy<T> class makes it really simple to implement laziness.

public class StateManager
{
    private static readonly Lazy<StateManager> obj = new Lazy<StateManager>(() => new StateManager());
    private StateManager() { }
    public static StateManager Instance
    {
        get
        {
            return obj.Value;
        }
    }
}

Take a look at the Instance property in the StateManager class above. Note that the Value property you see in the above code example is read-only. For that reason there is no set accessor.

Lazy initialization is an excellent performance optimization technique, allowing you to defer the initialization of objects that consume significant CPU and memory resources until you absolutely need them. Take advantage of lazy initialization to improve the performance of your apps.

Похожее
Jun 2, 2023
Cyclomatic complexity is a code metric (integer value 1 or more) used to measure how complex a function/method is.It does not take into account lines of code but instead considers complexity to be the distinct paths through a function.Microsoft defines...
Jul 25, 2023
Author: Kenji Elzerman
Web scraping is a technique that scrapes information from other online sources. This is a great way to combine different sources into one source. To create a web scraper you need a few things: online sources, some code that can...
Nov 16, 2022
Author: Vipin Sharma
Need to communicate with multiple DBs in one application??Usually we don’t face such scenarios. However if such requirement comes in picture, we will see how easy it is to communicate with multiple DBs using repository pattern in .Net Core Web...
Apr 7, 2023
Author: Matt Eland
Exploring nullability improvements in C# and the !, ?, ??, and ??= operators.Null is famously quoted as being the "billion-dollar mistake" due to the quantity of NullReferenceExceptions that we see in code (particularly when just starting out).The prevalence of null...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban