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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:28:58+00:00 2026-05-23T17:28:58+00:00

I have a generic class, on which I define a method that should take

  • 0

I have a generic class, on which I define a method that should take arguments of another type, but only if the other type implements the type parameter of the class. However, this won’t compile:

class GenericClass<TClass>
{
    public void DoSomething<TMethod>(TMethod input) where TClass : TMethod
    {
        // ...
    }
}

I get a compiler error on TClass in the constraint on the method. Is there another way to specify this?

Clarification:
I am under the impression that TMethod : TClass means that TMethod must inherit or implement TClass (depending on whether TClass is a concrete type or an interface). In other, slightly unconventional, notation TMethod > TClass (meaning TMethod is a superset of TClass).

What I want here, is that TMethod should only be a valid type if TClass inherits or implements it, i.e. TClass > TMethod. Will TMethod : TClass do that as well?

(One of the answers states that TMethod : TClass requires TMethod to be assignable from TClass. I’m not sure if that meets my requirements, but if it does, please explain in more detail what being assignable from means, since if it helps me I’ve probably misunderstood it…)

  • 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-23T17:28:59+00:00Added an answer on May 23, 2026 at 5:28 pm

    Executive summary:

    Chris Hannon’s answer is basically correct. However, there are sneaky tricks involving interfaces and extension methods that might get you what you want.

    Excessive details:

    We do occasionally get asked for this sort of constraint, and there are other languages that have constraints like this. Java and I believe Scala both have ways of saying “this has to be a supertype of that”. As others have pointed out, this sort of constraint cannot be represented in either the C# type system or the underlying CLR type system.

    The reason why you might want this sort of variance would be for a situation like this:

    class ImmutableStack<T>
    {
        public static ImmutableStack<T> Empty { get { return empty; } }
        private static ImmutableStack<T> empty = new ImmutableStack<T>();
        private T head;
        private ImmutableStack<T> tail;
        public ImmutableStack<T> Pop()
        {
            if (this == empty) throw new Exception();
            return this.tail;
        }
        public ImmutableStack<N> Push(N value) where T : N // not legal
        {
            var result = new ImmutableStack<N>();
            result.head = value;
            result.tail = this; // also not legal
            return result;
        }
    }
    

    And now you can do something like:

    Tiger tony = new Tiger();
    Elephant dumbo = new Elephant();
    ImmutableStack<Tiger> tigers = ImmutableStack<Tiger>.Empty;
    tigers = tigers.Push<Tiger>(tony);
    ImmutableStack<Mammal> mammals = tigers.Push<Mammal>(dumbo);
    

    And hey, now you can push an elephant onto a stack of tigers and it magically becomes a stack of mammals!

    This is not possible in C# as I’ve shown here for two reasons: first, because there is no such generic constraint, and second, because immutable class types are not covariant. (The assignment to the tail requires covariance.)

    However it is possible to do this in C# if you are sneaky. You can get around the covariance problem by representing everything as a covariant interface rather than a class. You can get around the “no such constraint” problem by moving the method in question out of the class proper and making it into an extension method! I give an example of how to do so here:

    http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx

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

Sidebar

Related Questions

I have a generic class that should allow any type, primitive or otherwise. The
I have a generic class that I'm trying to implement implicit type casting for.
I have a generic class, but I want my type to be forced to
I have a abstract generic class. I want to define a method inside there
I have a base class which is non-generic with a derived generic class. The
I have a generic class public MyClass<TContext, T> where TContext : DataContext that effectively
I'm trying to add another restriction on a method within a generic class. Is
I have a generic class in C# with 2 constructors: public Houses(params T[] InitialiseElements)
I have a generic class in my project with derived classes. public class GenericClass<T>
I have an abstract generic class BLL<T> where T : BusinessObject . I need

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.