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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:24:07+00:00 2026-05-20T10:24:07+00:00

I think there are people who may be able to answer this, this is

  • 0

I think there are people who may be able to answer this, this is a question out of curiosity:

The generic CreateInstance method from System.Activator, introduced in .NET v2 has no type constraints on the generic argument but does require a default constructor on the activated type, otherwise a MissingMethodException is thrown. To me it seems obvious that this method should have a type constraint like

Activator.CreateInstance<T>() where T : new() {
   ...
}

Just an omission or some anecdote lurking here?

Update

As pointed out, the compiler does not allow you to write

private T Create<T>() where T : struct, new()
error CS0451: The 'new()' constraint cannot be used with the 'struct' constraint

However, see comments a struct can be used as type argument to a generic method specifying a new() constraint. Under this circumstance the given answer seems the only valid reason to not constrain the method…

Thanks for looking over this!

  • 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-20T10:24:08+00:00Added an answer on May 20, 2026 at 10:24 am

    I could be wrong, but the main benefit as I see it is that it allows you to do something like this:

    // Simple illustration only, not claiming this is awesome code!
    class Cache<T>
    {
        private T _instance;
    
        public T Get()
        {
            if (_instance == null)
            {
                _instance = Create();
            }
    
            return _instance;
        }
    
        protected virtual T Create()
        {
            return Activator.CreateInstance<T>();
        }
    }
    

    Note that if Activator.CreateInstance<T> had a where T : new() constraint, then the Cache<T> class above would also need that constraint, which would be overly restrictive since Create is a virtual method and some derived class might want to use a different means of instantiation, such as calling a type’s internal constructor or using a static builder method.

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

Sidebar

Related Questions

I think there is a simple answer to this, but for some reason I
I think there are not many people who don't think that ASP.NET MVC is
I've been using the search function but I think there isn't any related question
I think this is a pretty simple question but I'm having trouble finding the
I know this may not be exactly a coder question, but I feel it
There seem to be multiple people who had prolbems with getting IE to hide
Is such a thing possible? Did the people who designed REST just think they
I have a statistics database for people who have visited. This table contains ID,
Ok, I think this question is at the wrong place and I'll head over
This is question I have still not been able to resolve, so maybe I

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.