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

  • Home
  • SEARCH
  • 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 1023735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:40:20+00:00 2026-05-16T11:40:20+00:00

I was looking for ways to do lazy initialization and found Lazy<T> which is

  • 0

I was looking for ways to do lazy initialization and found Lazy<T> which is included in .NET 4.

I was thinking of rolling my own implementation of Lazy<T> for .NET 3.5 (with a simpler multi-thread policy), and I bumped into the following problem:

Lazy has basically two types of constructors:

class Lazy<T> {

    public Lazy(){...} // ctor #1

which uses T’s default constructor for creating an instance of T, and

    public Lazy(Func<T> func){...} // ctor #2

which lets the caller decide how the instance of T is created.

Now here’s the problem:

If I want compile-time checking for the 1st ctor I will add a restriction

class Lazy<T> where T: new() {...}

at the class level. This will allow me to use new T() to create an instance; but this restriction is not necessary for the 2nd ctor, and worse, it also restricts the types I can use (to those with a default ctor)

If I want to be able to use any type with the 2nd ctor, I will not set any restriction, and in the 1st ctor will use reflection to make sure T does have a default ctor. This approach, however, will lack the compile-time check, and will only throw a runtime exception if the 1st ctor is used with the wrong type.

My question is: Can I get the best of both worlds?

Ideally, I would like to get the compile-time check for every use of ctor #1, but at the same time be able to use ctor #2 for types that don’t have a default ctor.

How does the Microsoft implementation do this? (I don’t readily have access to the .NET 4 sources or dlls).

EDIT: (After “Reflector-ing” the MS assembly)

I checked the reference implementation and it doesn’t do compile-time checks.
It uses reflection for the ‘default ctor’ case, of course accompanied by the runtime exception if things go bad.

  • 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-16T11:40:21+00:00Added an answer on May 16, 2026 at 11:40 am

    I expect the inbuilt implementation simply uses Activator.CreateInstance<T> for simplicity. The cleanest way I can think of cheating this is with a separate factory:

    // non-generic factory class with generic methods
    public static class Lazy {
        public static Lazy<T> Create<T>() where T : new() {
            return Create<T>(() => new T());
        }
        public static Lazy<T> Create<T>(Func<T> ctor) { ... }
    }
    public class Lazy<T> { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just looking at ways of getting named constants in python. class constant_list: (A_CONSTANT, B_CONSTANT,
I've been looking at ways to implement gmail-like messaging inside a browser, and arrived
I've been looking at ways people test their apps in order decide where to
I'm looking at ways to deploy a Ruby on Rails app (running on JRuby)
I'm currently looking for ways to create automated tests for a JAX-RS (Java API
I was looking at ways to authenticate users in a web app, but in
I'm looking for ways to display a single row of data as a single
I am looking for ways to find matching patterns in lists or arrays of
I'm looking for secure ways to pass data between a client running Flash and
I am looking in to ways to enable a site to basically have something

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.