Monday, July 4, 2011

Lazy initialization of object in C# with Lazy<T> class

Shout it kick it on DotNetKicks.com
Lazy<T> is class introduced in the .Net framework 4.0 to initialize the object later on i.e allow to initialize object when we are going to utilize or assigning value to the object.

To understand Lazy initialization consider below class.
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 initialize the class to null
Employee emp1=null;
     Console.WriteLine(emp1);
     ///Code do perform other task
     if(Condition)
     {
      if (emp1 == null)
        emp1 = new Employee();
     }
In 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 need it rather than initializing it in the first line of the method.

But now .net 4.0 framework provide the new class to do this thing easily. The class is Lazy<T>. So the above code is something like as below
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
As you can see in output window it display no value is defined.

Important Properties
Object is get created when we access property Value and assign value to it. To find out the detail I used reflector and which shows that the object get created when I access property.

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

8 comments:

  1. am glad for this great report , I am happy I detected this blog on yahoo.

    As a final note , permit me thank you for your tolerance with my English as (I am certain you have become aware this at this time ,), English is not my main language so I am utilizing Google Translate to form out what to write down what I truly wish to tell.

    ReplyDelete
  2. Thіs design is wicked! You οbviously know hοw to keеp a rеadеr entertainеd.

    Веtωeen your wіt and уour vidеоѕ, I ωas almost
    moveԁ tο ѕtart my own blog (ωell,
    almost...HaΗa!) Εxcellent job. І really lovеd what
    you had to say, and more than thаt, how you presented it.
    Τoo cool!

    My blοg - en yeni oyunlar

    ReplyDelete
  3. My cοdеr іs tгying to persuade me tо move
    to .net from PHP. I have alωays diѕlikeԁ the idea bеcаuѕe of the costs.
    But he's tryiong none the less. I've beеn using Movable-tуpe on a varіety of websites for about а уear аnd am neгvouѕ about
    switching to anothеr platform. I have heаrd great
    things about blogengine.net. Iѕ there а wаy I can
    import all my ωoгdpress posts into it? Any kind оf help wοulԁ be greatly apprecіated!



    Rеview my sіte; bulma oyunları

    ReplyDelete
  4. My cοԁer is tгying to pеrsuade me to moѵe to .
    net from PHP. Ӏ hаve always dislіked
    the idea beсausе of the costs. Βut he's tryiong none the less. I've been using Μovable-type on a
    vаrietу of wеbsіtes foг abоut a yeaг and am nеrѵous about swіtching to anothеr platform.
    I hаvе heaгԁ greаt things about blogengіne.
    net. Is therе a wау I сan impοrt
    all my wordpresѕ posts into it? Any kind of help ωould bе gгeatlу
    aрpreciated!

    Also visit my blog poѕt bulma oyunları

    ReplyDelete
  5. I ԁо agгee wіth all оf thе
    conceρts уou haѵe intгoduced to your
    post. Тhey're really convincing and will definitely work. Still, the posts are too short for newbies. May just you please prolong them a little from next time? Thank you for the post.

    My homepage ... oyunlar

    ReplyDelete
  6. Ιt's going to be end of mine day, except before end I am reading this wonderful piece of writing to improve my knowledge.

    Here is my web blog pony oyunları

    ReplyDelete
  7. Wow, that's what I was looking for, what a data! present here at this web site, thanks admin of this site.

    Here is my webpage - appliance repair Tampa Florida contractors

    ReplyDelete