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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:39:34+00:00 2026-05-24T16:39:34+00:00

Working with C# Generics you can have a class like this: class Foo<T> where

  • 0

Working with C# Generics you can have a class like this:

class Foo<T> where T:new() {}

Which means that the type T should have a constructor without parameters. It would be nice if we could have:

class Foo<T> where T : new(string)
{
    private T CreateItem()
    {
        string s="";
        return new T(s);
    }
}

Is there any reason that Microsoft haven’t added this feature to the language?

  • 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-24T16:39:34+00:00Added an answer on May 24, 2026 at 4:39 pm

    Is there any reason that Microsoft haven’t added this feature to the language?

    The feature you describe is a specific case of the of the more general feature “allow a constraint that requires a particular method to exist”. For example, you might say:

    void M<T>(T t) where T has an accessible method with signature double Foo(int)
    {
        double x = t.Foo(123);
    }
    

    We don’t have that feature in C# because features have to be justified by a cost-benefit analysis. That would be a pretty expensive feature from both a design and implementation point of view — a feature that would drive requirements onto not just C# but every .NET language. What’s the compelling benefit that justifies the feature?

    Moreover: suppose we did design that feature. How would it be implemented efficiently? The constraints in the generic type system have been carefully designed so that the jitter can generate efficient code once that can then be shared for every reference type. How would we generate efficient code for arbitrary method pattern matching? That sort of efficient dispatch is pretty straightforward when the method’s slot can be known at compile time; with this feature we would no longer have that advantage.

    The feature you want is the same feature, just with the kind of method restricted to a constructor.

    Remember, the purpose of generics is to let you write generically typed code. If you’re requiring constraints that are more specific than things that can be captured in the type system then you might be trying to abuse generics.

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

Sidebar

Related Questions

In a proof-of-concept project I'm working on, I have a generic abstract class that
I have implemented a templated buffer class like this: template <class T, class Impl>
I am currently working with .Net 2.0 and have an interface whose generic type
I am working on a class library and am having some trouble with generics.
I have written a program which will serialize and de-serialize, it does this fine
Imagine C# code like this ISomeInterface someVar = creator.Create(typeof(SomeClass), typeof(ISomeInterface)); What should happen? Method
Here is my problem. I would like to have a class with a private
I have a C# app I'm working on that loads it's code remotely, and
I'm trying to create a C# class that can call the same methods on
I'm new to Django... I have installed a Django external App called Haystack, this

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.