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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:10:41+00:00 2026-05-11T21:10:41+00:00

Is there a good, generic, way to do the following without resorting to a

  • 0

Is there a good, generic, way to do the following without resorting to a second method or lots of casts – I want to keep the API as light as possible and it seems ok to me OO-wise:

class Foo
{
  public T Bar<T>() where T: IAlpha
  {
    /* blahblahblah */
  }

  public T Bar<T>() where T: IBeta
  {
    /* blahblahblah */
  }
}

interface IAlpha
{
  string x {set;}
}

interface IBeta
{
  string y {set;}
}

thanks

  • 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-11T21:10:41+00:00Added an answer on May 11, 2026 at 9:10 pm

    You can’t overload a method by return value only (generic or not). Additionally, it would be impossible to resolve calls to Bar, since an object could implement both IAlpha and IBeta, so this is not possible using overloading.

    public class AlphaBeta : IAlpha, IBeta
    {
        string x {set;}
        string y {set;}
    }
    
    // too ambiguous
    AlphaBeta parkingLot = myFoo.Bar<AlphaBeta>();
    

    The below will also not work, because the methods only differ by return type

    class Gar
    {
        public string Foo()
        {
            return "";
        }
    
        public int Foo()
        {
            return 0;
        }
    }
    

    Unfortunately, your best solution will be to use a less generic solution. The command pattern might serve you well here.

    public class Foo
    {
        private readonly static Dictionary<Type, Command> factories =
            new Dictionary<Type, Command>();
    
        static Foo()
        {
            factories.Add(typeof(IAlpha), new AlphaCreationCommand());
            factories.Add(typeof(IBeta), new BetaCreationCommand());
        }
    
        public T Bar<T>()
        {
            if (factories.ContainsKey(typeof(T)))
            {
                return (T) factories[typeof(T)].Execute();
            }
            throw new TypeNotSupportedException(typeof(T));
        }
    }
    
    // use it like this
    IAlpha alphaInstance = myFoo.Bar<IAlpha>();
    IBeta betaInstance = myFoo.Bar<IBeta>();
    

    Another way of implementing Bar which allows you to call it without explicitly declaring the type (in your angled brackets) is to use an out parameter. I’d avoid it, however, since out parameters in 100% managed usually stink of bad design.

    public void Bar<T>(out T returnValue)
    {
        if (factories.ContainsKey(typeof(T)))
        {
            returnValue = (T) factories[typeof(T)].Execute();
            return;
        }
        throw new TypeNotSupportedException(typeof(T));
    }
    
    // call it like this
    // T is inferred from the parameter type
    IAlpha alphaInstance;
    IBeta betaInstance;
    myFoo.Bar(out alphaInstance);
    myFoo.Bar(out betaInstance);
    

    I excluded Command, AlphaCreationCommand, BetaCreationCommand, and TypeNotSupportedException. Their implementation should be fairly self explanatory.

    Alternately, you can use Func instead of Commands, but this forces you to implement all of your instantiation code in Foo which can get out of hand as your code base grows.

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

Sidebar

Related Questions

Is there a good way to keep keys from conflicting when using the Microsoft
Is there a generic way, without creating and managing your own CLR host, to
Is there a good way to start at a given index and move BACKWARDS
Is there any good solution for the following requirements: A form with one field
Is there a good way to emulate yield in Ruby? I'm interested in writing
Is there a good way to do this? I'm writing an extension that interacts
is there any generic way to get the role which is required for some
Possible Duplicate: Generating all Possible Combinations Is there a good LINQ way to do
I want to implement a generic method to retrieve header/detail data from a database:
Is there a good way in C++ to implement (or fake) a type for

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.