Lazy initialization, also known as lazy instantiation, is an OOP pattern that involves delaying the creation of an object until its first use. Think of a restaurant where food is made on demand which can help to prevent the waste of resources.
This is the same principle of lazy initialization. Its main goal is to improve the performance and avoid wasteful computation resources.
In this guide, we will cover the basics of lazy initialization pattern in C#. Keep in mind that we will explore the most essential and skip the advanced implementation of this pattern such as the creation of custom lazy initialization and implementation, threading the lazy initialization, and more.
Lazy Initialization in C#
In C#, we can implement the lazy object initialization using the Lazy<T> class. This class provides us with the features to implement the lazy initialization.
It is good to keep in mind that C# allows us to implement the custom methods of building the lazy initialization objects. However, for our case, we will use the Lazy<T> class which has support for thread-safety.
The syntax is as follows:
Here, “T” denotes the type of object that we wish to lazily initialize.
Example Usage:
Take the following code that demonstrates how we can use the Lazy<T> class to perform the lazy object initialization:
class ResourceIntensive
{
public ResourceIntensive()
{
Console.WriteLine("Resource intensive operation created!");
}
public string GetData()
{
return "Data from Resource intensive operation.";
}
}
class Program
{
static void Main()
{
Lazy<ResourceIntensive> lazyResource = new Lazy<ResourceIntensive>();
Console.WriteLine("Lazy<ResourceIntensice> created.");
Console.WriteLine("Is value created? " + lazyResource.IsValueCreated);
// pay attention here
ResourceIntensive resource = lazyResource.Value;
Console.WriteLine("Is value created? " + lazyResource.IsValueCreated);
Console.WriteLine(resource.GetData());
}
}
In our given example, pay attention to the “ResourceIntensive” class. This helps us to simulate an operation that consumes a lot of resources.
In the main function, we start by creating a Lazy<ResourceIntensive> object but we do not call the ResourceIntensive constructor right away.
We then check and print if the value inside the Lazy<ResourceIntensive> object is ready.
In the next step, we attempt to access the “Value” property of the Lazy<ResourceIntensive> object. Doing this triggers the creation of the actual object.
Finally, we re-check the value of the object.
This should return an output:
Is value created? False
Resource intensive operation created!
Is value created? True
Data from Resource intensive operation.
Did you understand what went on?
What this does is essential delay the object creation unless it is absolutely needed. In our case, the use of this object is raised when we attempt to access the “Value” property.
If this hadn’t tried to access this property, the object would have remained uninitialized until we do. As you can notice, this prevents unnecessary waste of precious system resources.
Conclusion
In this fundamentals tutorial, we attempted to explain and help you understand the concept of lazy initialization in C#, why it’s necessary, and when you might use it.