If you are using Microsoft .NET 4.0 or higher, it comes with an excellent feature called Lazy Initialization. .NET 4.0 introduces a “Lazy<T>” class to support the lazy initialization, where “T” specifies the type of object that is being initialized lazily. This is a new feature of C# 4.0 and can be used when we are working with large objects.
To learn more about it, let’s create a Console application first and then create a class named “Person”. Now create a custom constructor that takes a parameter “id” as integer. In the constructor implementation, write a line to the console as “Constructor of Person <ID> has been called”. Here is the code for your reference:
Code:
public class Person
{
public Person(int id)
{
Console.WriteLine("Constructor of Person " + id + " has been called");
}
}
Now, as per default implementation, it will print the line every time when you create an instance of the class. Right? Don’t go with my words. Let’s see that in action. Let us create an array of six Person and then get the instance of the 3rd person from that collection. Need the code? Here it is for you:
Code:
class Program
{
static void Main(string[] args)
{
var persons = new[]
{
new Person(1),
new Person(2),
new Person(3),
new Person(4),
new Person(5),
new Person(6),
};
var person = persons[2]; // this will return the instance of Person 3
Console.ReadLine(); // just to make sure that the application halts here
}
}
As per the default behaviour, it will print six lines in the console screen. Each line represents creation of a single instance. You can check the ID to cross check it. The output will look similar to this as per our code:
Output:
Constructor of Person 1 has been called
Constructor of Person 2 has been called
Constructor of Person 3 has been called
Constructor of Person 4 has been called
Constructor of Person 5 has been called
Constructor of Person 6 has been called
So, think about the thousands of records! This will create instances of those many records in the memory unnecessarily without any need. User might not go to the end of the collection but the instances of the object is ready. Maximum time this creates issues in WPF, Silverlight DataGrid or List.
Now, let us modify our existing code and build the lazy initialization to it. Now instead of a Person array, we will create an array of type Lazy<Person>, that’s all! The implementation step is as shown below:
Code:
class Program
{
static void Main(string[] args)
{
var persons = new[]
{
new Lazy<Person>(() => new Person(1)), //Lasy<T> is important
new Lazy<Person>(() => new Person(2)),
new Lazy<Person>(() => new Person(3)),
new Lazy<Person>(() => new Person(4)),
new Lazy<Person>(() => new Person(5)),
new Lazy<Person>(() => new Person(6)),
};
var person = persons[2].Value; // this will return the instance of Person 3
Console.ReadLine(); // just to make sure that the application halts here
}
}
If you need to fetch the 3rd person only, get it from “Value” property of the item from the array collection. So, to get the instance of person with ID=3, call person[2].Value, this will return the instance of Person 3.
Let’s run the code this time. Voila! It just called only the constructor of Person 3 this time. That means, it created the instance of person 3 when we actually used it. The other instances in the array have not been created yet. So, if we call any other instance, at that time only it will create that particular instance. Check out the below screenshot:
Output:
Constructor of Person 3 has been called
It will not it save lot’s of processing and memory when your application runs. This feature is called as “Lazy Initialization” or “Lazy Loading”.
No comments:
Post a Comment