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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:37:55+00:00 2026-05-16T17:37:55+00:00

The type argument must have a public parameterless constructor. When used together with other

  • 0

The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.

Can you guys give me a sample scenario when this constraint is needed?

  • 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-16T17:37:56+00:00Added an answer on May 16, 2026 at 5:37 pm

    This is essentially what the new() constraint boils down to:

    class Factory<T> where T : new()
    {
        public T Create()
        {
            return new T();
            //     ^^^^^^^
            //     this requires the new() type constraint.
        }
    }
    

    Now, you’re not allowed to pass arguments to the constructor. If you nevertheless want to initialize the newed object, you could achieve this e.g. by introducing a further constraint:

    interface ILikeBananas
    {
        double GreenBananaPreferenceFactor { get; set; }
    }
    
    class Factory<T> where T : ILikeBananas, new()
    {
        public T Create(double greenBananaPreferenceFactor)
        {
            ILikeBananas result = new T();
            result.GreenBananaPreferenceFactor = greenBananaPreferenceFactor;
    
            return (T)result;
            //     ^^^^^^^^^
            //     freely converting between ILikeBananas and T is permitted
            //     only because of the interface constraint.
        }
    }
    

    Note that another way of instantiating an object is via Activator.CreateInstance, which gives you some more freedom, such as passing arguments directly to a constructor.

    Activator.CreateInstance does not strictly require the new() constraint; however, the type being instantiated still needs to provide a suitable constructor.

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

Sidebar

Related Questions

I know that non-type template argument for intgral type must be const expression so:
I have these codes: class Type2 { public: Type2(const Type1 & type); Type2(int); const
I have a type that is derived from an entity: public class WidgetB :
I have a class which must be able to hold a type of float,
I am getting a Wrong type argument: commandp, (lambda nil (forward-line 5)) here. (global-set-key
Do these two statements pass the same type of argument (a Hash) to the
In the following code, I get a compile time error: ByRef Argument type mismatch.
I am passing and argument of type string to an AJAX call. The value
I made a code that parse Function but it won't parse the Argument (name,type)
Can I use type as a name for a python function argument? def fun(name,

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.