Link Search Menu Expand Document

Fundamentals of Lazy Initialization in C# - Part-1

What is Lazy Initialization? Lazy initialization of an object means that it is a technique that defers the creation of an object until the first time it is needed. In other words, You can say initialization of the object happens only on demand. In this video, we’ll learn the basics of lazy keywords in C#.

Have you ever wondered that in #C you can defer the creation of an object until it is actually used? In this session, we will be going to learn about how we can lazily initialize the objects using the newly introduced lazy keyword of #C. Well, with #C 4.0 Microsoft has introduced a lazy keyboard using which initialization of the object happens only on demand. This feature comes in handy when we are working with large objects. Let’s dive deeper and explore this lazy keyword by performing some demo. To perform a demo here, I have defined one console application in which I define one patient object class. Now, if we have a look at this class, we can see that we have got our public default constructor and we are just writing out some information to the console using the console dot writeline. So overall, this is a very simple class that contains a fewer getter setter property for patient basic detailed information.

If we run this application, it simply creates an object and calls its constructor and we get our message in the console. Now, consider a situation where the creation of these objects in the constructor requires some resource-intensive activity. If we don’t always use all of the objects we create, we are still going to incur that ghost when we create that object. By using lazy initialization, the objects are not actually constructed until the first time we try to access them. To create a lazy version of this patient detail class, I need to use a lazy class [var lazyPatientDetail = new Lazy<PatientDetail>();]. Let me comment on this class. Now to check whether the object of this glass is created or not, there is one property called ‘is value created in this object’ using which we can check the object status.

Programming Essentials

If I run this application and when I check this property value, you can see that this value is false. So even though we have executed this line, the constructor of this class has still not been executed, which you can see in this console window. Now the question is how we can initialize the lazy object? Well, it’s very simple. What you need to do is just access the value property of this lazy object. Then from here, let’s call this method. This time, if I run this application, you can see that this property has now become true. Here you can see that first the constructor is called and then our method is called. Then finally we get this message.

Essentials for Developers

Now, what happens if we have an overloaded constructor that takes a patient ID as an argument? To execute this overloaded constructor, we can specify the constructing function of this lazy class like this. So as you can see there, this overload takes a function that creates a new patient detail object but this time using the overloaded constructor, which allows us to specify the patient ID.

Run this application and check the output. Here we can clearly see that this time the parameterized constructor is called with this patient ID. Here, regardless of which of these constructors we use in this class, the actual initialization is not performed until the first time we access this valuable property. What if multiple threads are racing to initialize this object? We learn that in the next session and we will also do some performance testing with this lazy class. So till then happy learning and stay tuned with us.

For more videos and articles visit: