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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:01:07+00:00 2026-06-18T02:01:07+00:00

I have a base class that defines a generic method like this: public class

  • 0

I have a base class that defines a generic method like this:

public class BaseClass
{
    public T DoSomething<T> ()
    { ... }
}

As this class is by a third-party and does not come with an interface, I am defining an interface that defines the actually needed methods from that class. That way I get loose coupling and can actually exchange that third-party class with something else. For this example, consider the following interface:

public interface ISomething
{
    T DoSomething<T> ()
        where T : Foo;
}

As you can see, it defines the same method but also applies a type constraint on the type parameter, which comes from some other requirements that are not relevant to this.

Next, I define a subtype of BaseClass which also implements ISomething. This class will be used as the usual implementation behind the interface–while the interface will be what the rest of the application will be accessing.

public class Something : BaseClass, ISomething
{
    // ...
}

As the DoSomething in BaseClass already supports any type parameter T, it should especially support a type parameter which is a subtype of Foo. So one would expect that a subtype of BaseClass already implements the interface. However I get the following error:

The constraints for type parameter ‘T’ of method ‘BaseClass.DoSomething()’ must match the constraints for type parameter ‘T’ of interface method ‘ISomething.DoSomething()’. Consider using an explicit interface implementation instead.

Now, I have two possibilities; the first one is to do what the error suggests and implement the interface explicitely. The second is to hide the base implementation using new:

// Explicit implementation
T ISomething.DoSomething<T> ()
{
    return base.DoSomething<T>();
}

// Method hiding
public new T DoSomething<T>()
    where T : Foo
{
    return base.DoSomething<T>();
}

Both work, although I’d probably prefer the second solution to keep the method accessible from the class itself. However it still leaves the following question:

Why do I have to re-implement the method when the base type already implements it with a less-strict (read: none) type constraint? Why does the method need to be implemented exactly as it is?

edit: To give the method a bit more meaning, I changed the return type from void to T. In my actual application, I have both generic arguments and return values.

  • 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-06-18T02:01:08+00:00Added an answer on June 18, 2026 at 2:01 am

    Certainly the given code could be compiled and run safely:

    When a Something instance is typed as Something or as BaseClass the compiler would allow any type for T, while when the same instance is typed as ISomething it would allow just types inheriting Foo. In both cases you get static checking and runtime safety as usual.

    In fact, the above scenario is exactly what happens when you implement ISomething explicitly. So let’s see what arguments we can make for and against the current state of affairs.

    For:

    • the proposed solution would not be applicable to all cases; it depends on the exact method signatures (is the type argument covariant? contravariant? invariant?)
    • it doesn’t require the specification to be amended with new text stating how such cases are handled
    • it makes the code self-documenting — you don’t have to learn said text; the current rules regarding explicit interface implementation are enough
    • it does not impose development costs on the C# compiler team (documentation, feature implementation, testing, etc)

    Against:

    • you need to type more

    Considering the above and additionally the fact that this is not an everyday scenario, IMHO the conclusion to be reached is clear: this might be nice to have, but it certainly doesn’t warrant going out of your way to implement it.

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

Sidebar

Related Questions

I have a Base class that needs a Generic type. That can be either
I have a Generic Base Class that I want to allow one of two
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a class that inherits from a base class (this contains a lot
I have confusing situation. Base Generic Type and successor public abstract class BaseType<TEntity> :
I have some abstract base class that defines abstract functions. For example (barebones), abstract
I have a class that does some generic code for Writing to a Database.
I have a base class ( car ) and a class that inherit the
Suppose I have a Base class and a Child class that inherits from Base
In the project I'm working on now, we have base Entity class that looks

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.