Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

Lazy Initialization of Object in C# with Lazy Class

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
10 Aug 2011CPOL1 min read 40.6K   4
Lazy initialization of object in C# with Lazy class

Lazy<T> is a class introduced in the .NET Framework 4.0 to initialize the object later on, i.e., allows to initialize object when we are going to utilize or assign value to the object.

To understand Lazy initialization, consider the below class:

C#
public class Employee
    {
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Address { get; set; }
        public int Id { get; set; }
    }

In the following code, I am using the Employee class. As you can see, I have initialized the class to null.

C#
Employee emp1=null;
     Console.WriteLine(emp1);
     ///Code do perform other task
     if(Condition)
     {
      if (emp1 == null)
        emp1 = new Employee();
     }

In the above code, I am initializing class when I need it rather than initializing before using it same way we can do in the Singleton design pattern. By this way, we are lazing initializing the object and consume memory when we need it rather than initializing it in the first line of the method.

But now, .NET 4.0 Framework provides the new class to do this thing easily. The class is Lazy<T>. So the above code is something as below:

C#
Lazy<employee> emp = new Lazy<employee>();
     Console.WriteLine(emp);

     emp.Value.Id = 1;
     emp.Value.Name = "pranay";
     emp.Value.Salary = 123;
     emp.Value.Address = "Ahmedabad";
     Console.WriteLine(emp);

Output of the program is as follows:

Image 1

As you can see in the output window, in its display, no value is defined.

Important Properties

Object gets created when we access property Value and assign value to it. To find out the details, I used reflector which shows that the object gets created when I access property.

Image 2

Another important property is IsValueCreated - Gets a value that indicates whether a value has been created for this Lazy<t> instance.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
QuestionHow Call Constructor with Lazy Instantiation of Object ? Pin
Ammar Shaukat23-Apr-17 21:07
professionalAmmar Shaukat23-Apr-17 21:07 
QuestionFeedback Pin
MK-Gii24-Jul-13 20:22
MK-Gii24-Jul-13 20:22 
GeneralMy vote of 5 Pin
hoernchenmeister1-Aug-11 21:14
hoernchenmeister1-Aug-11 21:14 
GeneralRe: My vote of 5 Pin
Pranay Rana11-Aug-11 0:26
professionalPranay Rana11-Aug-11 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.