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

  • Home
  • SEARCH
  • 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 987647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:30:46+00:00 2026-05-16T05:30:46+00:00

In generics we can give constraints using the where clause like public void MyGeneric

  • 0

In generics we can give constraints using the “where” clause like

public void MyGeneric <T>() where T : MyClass1 {...}

Now if i want the type T to be of type MyClass1 and say an interface IMyInterface then i need to do something like

public void MyGeneric <T>() where T : MyClass1, IMyInterface  {...}

But I dont know (or maybe it is not possible) if we can create a generic method that can take types which inherits from either of the 2 types. i.e. if any of my other classes inherits from MyClass1 or implements IMyInterface but neither of my class has both then my generic method should work for these classes.

I hope I have been clear.

  • 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-16T05:30:47+00:00Added an answer on May 16, 2026 at 5:30 am

    You can’t, and for a good reason. Say MyClass1 and IMyInterface both have a CoolThing() method (presumably such commonality is precisely why you want to do this sort of thing in the first place). You sort of want code like:

    public void MyGeneric<T>(T item) where T : MyClass1 or T : IMyInterface
    {
      item.CoolThing();
    }
    

    The problem with this is that as MyClass1.CoolThing() is defined completely differently to IMyInterface.CoolThing(). You may know that they do essentially the same thing, but they may be very different indeed (Employee.Fire() is presumably very different to Gun.Fire() and so on).

    You’ve got two options that I can see. One is to define two different methods. Overloading will reduce the headache of calling them, and they could share some pieces of their implementation in a private method that doesn’t depend upon the relevant features of the MyClass1 and IMyInterface signatures.

    The other is to use reflection to obtain the MethodInfo of the method called “CoolThing” on the object in question, and then invoke it. This latter obviously blows away your compile time type safety.

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

Sidebar

Related Questions

I like generics a lot and use them whereever I can. Every now and
can ASP.NET controls be used with generics? Never seen this done and want a
In C# we can define a generic type that imposes constraints on the types
How can I access Class object for Generics? Currently, I am doing it this
I'm stuck with .Net 1.1 application (i.e. I can not use the generics goodies
How can I make this java generic cast ? public interface IField { }
Are there any more generic tools that can compile or basically merge multiple PHP
How can I initialize a list containing generic objects whose types can be different?
How can I use reflection to create a generic List with a custom class
Can anyone suggest a good implementation of a generic collection class that implements the

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.