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 8394723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:07:52+00:00 2026-06-09T20:07:52+00:00

What are the differences between LazyInitializer and Lazy<T> classes? I know they both will

  • 0

What are the differences between LazyInitializer and Lazy<T> classes?
I know they both will initialize the object only on demand.
When do I need to use each of them?

  • 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-06-09T20:07:54+00:00Added an answer on June 9, 2026 at 8:07 pm

    Lazy<T> (MSDN) is a generic wrapper which allows creating an instance of T on demand by holding a T factory method (Func<T>) and calling it when Value property getter is accessed.

    LazyInitializer – static class with a set of static methods, this is just a helper which uses Activator.CreateInstance() (reflection) able to instantiate a given type instance. It does not keep any local private fields and does not expose any properties, so no memory usage overheads.

    Worth noting that both classes uses Func<T> as instance factory.

    MSDN says in few words about LazyInitializer class:

    These routines avoid needing to allocate a dedicated,
    lazy-initialization instance, instead using references to ensure
    targets have been initialized as they are accessed.

    PS:
    I found interesting a way how LazyIntiializer checks whether instance already initialized, it just compare a passed in reference to a default(T), nice:

    private static T EnsureInitializedCore<T>(ref T target, Func<T> valueFactory) 
        where T : class
    {
        T t = valueFactory();
        if (t == null)
        {
           throw new InvalidOperationException(Environment.GetResourceString("Lazy_StaticInit_InvalidOperation"));
        }
    
        Interlocked.CompareExchange<T>(ref target, t, default(T));
        return target;
    }
    

    What seems strange to me, it creates a new instance each time before an actual check:

    T t = valueFactory(); 
    // ... and only then does check
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering what differences between web service and ssh are? Are they both
What are the differences between windows service and windows driver ? Don't they both
What are the differences between UTF-8, UTF-16, and UTF-32? I understand that they will
The only differences between randrange and randint that I know of are that with
What are the main differences between these WPF controls? And when I should use
I know the differences between IBAction and IBOutlet, but I don't know when I
Can I understand the differences between == and object.equal(Object obj) in the following way?
What are the differences between Membership.GetUser() and Context.User, and which is recommended for use
I know the differences between data , newtype and type very well. I am
Are there any practical differences between special forms and macros? In what do they

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.