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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:51:28+00:00 2026-05-26T02:51:28+00:00

In C#, given a generic type such as this: interface IGenericType<T> where T :

  • 0

In C#, given a generic type such as this:

interface IGenericType<T> where T : new()

And a descendant type, such as:

class GenericTypeImplementation<U> : IGenericType<U>

Why do we need to explicitly restrict the generic type U with all the restrictions of the parent type?

class GenericTypeImplementation<U> : IGenericType<U> where U : new()

Am I right in inferring that the issue is in the compiler computing the union of restrictions?

interface IGenericType<T> where T : new()
interface IGenericType2<T> where T : SomeOtherType
class GenericTypeImplementation<U> : IGenericType<U>, IGenericType2<U>
/* Hypothesis: Compiler can't infer U must be "SomeOtherType + new()" */
  • 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-26T02:51:29+00:00Added an answer on May 26, 2026 at 2:51 am

    In my opinion, the compiler could be smart enough to infer the restrictions theoretically. But it shouldn’t be so smart, because a too-smart compiler is sometimes dangerous. Developers always need a clear/explicit definition of everything. See this scenario:

    (1) there is an interface IFoo<T> where T : new()

    (2) a class Foo<T> : IFoo<T> and the new() constraint is added automatically by the compiler(brilliant!)

    (3) the class Foo<T> is a very base class in the whole project, class A<T> : Foo<T>, and then class B<T> : A<T>…

    (4) Now another developer can hardly realize there is such a constraint by looking into the definition of the class, he will get weird compiling errors(well that’s acceptable). But what if they are invoked by reflection? Sometimes the program is correct, because the data meets the restriction by accident.

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

Sidebar

Related Questions

Given a type such as: public class FooList : List<Foo> { public string SomeMessage
Given a generic interface such as the following interface I<T> { void m(T t);
I've got a System.Generic.Collections.List(Of MyCustomClass) type object. Given integer varaibles pagesize and pagenumber, how
Trying to find out if a provided Type is of a given generic type
In C#, I can implement a generic interface twice on one class, using two
If have a type hierarchy that starts out with a generic type class A<T>
When we have something like this: interface ISomething<U,V> { ... } class Something<U,V> :
Given the following CRTP type in C#: public abstract class DataProviderBase<TProvider> where TProvider :
I want store generic controls in variable without knowing its type. By I need
Given a class hierarchy where the base class defines a recursive self-type: abstract class

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.