Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6057151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:27:24+00:00 2026-05-23T08:27:24+00:00

What is lazy initialization. here is the code i got after search google. class

  • 0

What is lazy initialization. here is the code i got after search google.

class MessageClass
{
    public string Message { get; set; }

    public MessageClass(string message)
    {
        this.Message = message;
        Console.WriteLine("  ***  MessageClass constructed [{0}]", message);
    }
}

Lazy<MessageClass> someInstance = new Lazy<MessageClass>(
    () => new MessageClass("The message")
    );

Why should I create an object in this way?
When actually we need to create object in this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:27:24+00:00Added an answer on May 23, 2026 at 8:27 am

    The purpose of the Lazy feature in .NET 4.0 is to replace a pattern many developers used previously with properties. The “old” way would be something like

    private MyClass _myProperty;
    
    public MyClass MyProperty
    {
        get
        {
            if (_myProperty == null)
            {
                _myProperty = new MyClass();
            }
            return _myProperty;
        }
    }
    

    This way, _myProperty only gets instantiated once and only when it is needed. If it is never needed, it is never instantiated. To do the same thing with Lazy, you might write

    private Lazy<MyClass> _myProperty = new Lazy<MyClass>( () => new MyClass());
    
    public MyClass MyProperty
    {
        get
        {
            return _myProperty.Value;
        }
    }
    

    Of course, you are not restricted to doing things this way with Lazy, but the purpose is to specify how to instantiate a value without actually doing so until it is needed. The calling code does not have to keep track of whether the value has been instantiated; rather, the calling code just uses the Value property. (It is possible to find out whether the value has been instantiated with the IsValueCreated property.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm adding some lazy initialization logic to a const method, which makes the method
Consider the double-check idiom for lazy initialization of instance fields: // Item 71 in
What is Lazy Loading? [Edit after reading a few answers] Why do people use
I'm perhaps being a bit lazy asking this here, but I'm just getting started
i am using the following approach to solve lazy initialization problem in hibernate. Please
i am using the following approach to sole lazy initialization problem in hibernate.Pleas tell
I was thinking about the classic issue of lazy singleton initialization - the whole
In a recent project I had to create a Singleton class and after a
I was looking for ways to do lazy initialization and found Lazy<T> which is
In this code, why has been written Not thread safe? Thank you class Singleton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.