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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:05:54+00:00 2026-06-04T14:05:54+00:00

I have a generic method: public bool DoSomething<T>(T item) where T: IBase { return

  • 0

I have a generic method:

public bool DoSomething<T>(T item) where T: IBase
{
    return DoSomethingSpecific(item);
}

public bool DoSomethingSpecific(IBase item)
{
    return true;
}

public bool DoSomethingSpecific(IAdvanced item)
{
    return false;
}

Note that IAdvanced interface derives/inherits from IBase interface.

I have found that if I call DoSomething where the item is of type IAdvanced, it still always returns false. I don’t understand this. I know that since IAdvanced is a type IBase (as it is a child of this interface), it may cause confusion between the 2 overloaded types of the DoSomethingSpecific method. However, as I understand with my limited C# knowledge, the IAdvanced method should be chosen here. This is an example of how I made this conclusion:

public class Advanced: IAdvanced
{

   public void CallMethod()
   {
      DoSomething(this);
   }
}

This results in a true value.

However, if I do:

public class Advanced: IAdvanced
{

    public void CallMethod()
    {
       DoSomethingSpecific(this);
    }
}

It returns false, which is what I would expect.

I have to say that I have never used generics before. I have attempted though, but always get stuck on a case like this, and then completely fail to see the point of using generics (besides data structures such as trees and linked lists).

This time I decided to come here for some advice. Is there a clear problem with what I am trying to do? Does it perhaps not make sense to try and do what I am busy doing here?

  • 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-04T14:05:55+00:00Added an answer on June 4, 2026 at 2:05 pm

    As far as it knows, it’s an IBase. The compiler needs to deside which method are you calling and that’s why it’s choosing that one always.

    A dirty trick will be to do this:

    public static bool DoSomething<T>(T item) where T: IBase
    {
        var isAdvanced = typeof(IAdvanced).IsAssignableFrom(typeof(T));
        return isAdvanced ? DoSomethingSpecific((IAdvanced)item) : DoSomethingSpecific(item);
    }
    

    Another way is to use Double-Dispatch/Visitor pattern:

    public interface IDoSomethingVisitor {
        bool DoSomethingSpecific(IBase base);
        bool DoSomethingSpecific(IAdvanced adv);
    }
    

    The DoSomething Method will be in your IBase interface:

    public interface IBase{
        void DoSomething(IDoSomethingVisitor visitor);
    }
    

    And in your implementations:

    public class Base : IBase
    {
       public bool DoSomething(IDoSomethingVisitor visitor)
       {
          visitor.DoSomething(this);
       }
    }
    
    public class Advanced : IAdvanced
    {
       public bool DoSomething(IDoSomethingVisitor visitor)
       {
          visitor.DoSomething(this);
       }
    }
    

    In this case, the problem is solved using pure inheritance. The actual instance is the one that resolves which method to call. No ifs.

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

Sidebar

Related Questions

I have the following method: public bool IsValid { get { return (GetRuleViolations().Count() ==
I have this generic method in my repository: public T GetFirstOrDefault(Expression<Func<T, bool>> where, Expression<Func<T,
I have the following generic method: public bool Any<TEntity>(Expression<Func<TEntity, bool>> whereCondition) where TEntity :
I have two classes: Superclass and derived Subclass:Superclass. I have a generic method: public
I have forgotten the syntax for a generic method: public static void swap <T>
I have a generic method define as below public T MyMethod<T>(extra params) My method
I have this method to get a generic repository out of a dictionary: public
I have a generic Print method that iterates over a list and simply prints
I have a C# string object that contains the code of a generic method,
I have the following method that is supposed to be a generic Save to

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.