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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:39:10+00:00 2026-05-22T00:39:10+00:00

So here’s what I’m trying to do. I’m creating a generic class that allocates

  • 0

So here’s what I’m trying to do.

I’m creating a generic class that allocates the type specified by the generic parameter in one of two ways, determined by which overloaded constructor is used.

Here is the example:

class MyClass<T>
    where T : class
{
    public delegate T Allocator();
    public MyClass()
    {
        obj = new T();
    }

    public MyClass( Allocator alloc )
    {
        obj = alloc();
    }

    T obj;
}

This class requires that type T is a reftype in all cases. For the default constructor, we want to instantiate T via its default constructor. I’d like to put a where T : new() on my default constructor, like this:

public MyClass()
    where T : new()
{
    obj = new T();
}

However, this is not valid C#. Basically I only want to add the constraint on type T to have a default constructor only when the default constructor of MyClass() is used.

In the second constructor for MyClass, we let the user determine how to allocate for T with their own allocation method, so obviously it makes sense for MyClass to not enforce T be default constructible in all cases.

I have a feeling that I’ll need to use reflection in the default constructor for this, but I hope not.

I know this can be done because the Lazy<T> class in .NET 4.0 does not require T to be default constructible at the class level, yet it has constructors similar to those in my example. I’d like to know how Lazy<T> does it at least.

  • 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-22T00:39:11+00:00Added an answer on May 22, 2026 at 12:39 am

    You can only include constraints in the declaration where you’re introducing a generic type parameter.

    However, you could introduce a generic method on a non-generic type:

    public class MyClass
    {
        public static MyClass<T> Create<T>() where T : class, new()
        {
            return new MyClass<T>(() => new T());
        }
    }
    
    public class MyClass<T> where T : class
    {
        T obj;
    
        public MyClass(Allocator allocator)
        {
            obj = allocator();
        }
    }
    

    (I’d personally just use Func<T> instead of declaring a separate delegate type btw.)

    Then you can use:

    MyClass<Foo> foo = MyClass.Create<Foo>(); // Enforces the constraint
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my scenario: $foo = bar; one() { two() { three() { // need
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here's what I'm trying do to in a single SQL Server procedure: @ID1 int
Here’s a problem I’ve really been struggling with. I need to merge two sorted

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.