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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:40:20+00:00 2026-05-13T19:40:20+00:00

I am creating a custom generic class: class Widget< T1, T2> { … public

  • 0

I am creating a custom generic class:

class Widget< T1, T2>
{
    ...
    public bool Bar( T1 type1 )
    {
        ...
    }
    public bool Bar( T2 type2 )
    {
        ...
    }
    ...
}

The following lines, of course, creates an ambiguous call compile error:

Widget<int, int> Foo = new Widget<int, int>();
...
Foo.Bar(5);
...

Is there any way around this? Is there a clause that I can put along the lines of “where : TypeOf(T1) != TypeOf(T2)”, or any way to make this disambiguous? Preferably int,int would be available, but it is not manditory.

Update:

I actually on my own discovered an acceptable solution (for me) to this problem, for those who are interested

class Widget< T1, T2>
{
    ...
    public bool Bar( object o )
    {
        if( o.GetType() == typeof(T1) )
        {
            ...
        }
        if( o.GetType() == typeof(T2) )
        {
            ...
        }
    }
    ...
}
  • 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-13T19:40:21+00:00Added an answer on May 13, 2026 at 7:40 pm

    Is there a clause that I can put along the lines of “where : TypeOf(T1) != TypeOf(T2)”

    You could make your constructor throw an exception at runtime. But there’s no way to prevent this situation at compile time.

    Any way to make this unambiguous?

    You should change the names of your methods so that they do not collide. That is by far the safest and easiest thing to do.

    In fact, IIRC the CLR reserves the right to fail to create a type that produces an ambiguity in method signatures like that. (Obviously our implementation actually does succeed, but you are treading on very thin ice when you pull these sorts of shenanigans.)

    Doing this kind of thing is a really, really bad idea because it can get you into all sorts of trouble. Here’s an example of how things go terribly wrong:

    http://blogs.msdn.com/ericlippert/archive/2006/04/05/odious-ambiguous-overloads-part-one.aspx

    http://blogs.msdn.com/ericlippert/archive/2006/04/06/odious-ambiguous-overloads-part-two.aspx

    Also note that the compiler will stop you from creating a type such that it implements two interfaces that could be identical under construction. This is illegal:

    class C<T, U> : IFoo<T>, IFoo<U> { ... }
    

    because you could then construct C<int, int> and the CLR would have no way of knowing which methods corresponded to which interface slots.

    But I seem to have digressed somewhat. Back on topic.

    Since you are the creator of this class, you can choose to rename your “Bar” methods so that they are different under any possible construction. Suppose you stubbornly choose not to. Is there anything that the user of your unfortunate class can do if they want to make Widget<int, int>? Yes, actually, there is, as kvb points out. They can define extension methods that do the right thing.

    public static void BarTheFirst<A, B>(this Widget<A, B> w, A a)
    {
        w.Bar(a);
    }
    
    public static void BarTheFirst<A, B>(this Widget<A, B> w, B b)
    {
        w.Bar(b);
    }
    

    Overload resolution is done at compile time, and at compile time all we know is that the first one calls the Bar that takes an “A”, and the second one calls the Bar that takes a “B”. We do not re-do overload resolution at runtime, so now you can say

    Widget<int, int> w = whatever;
    w.BarTheFirst(5);
    w.BarTheSecond(10);
    

    and it will do the right thing.

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

Sidebar

Ask A Question

Stats

  • Questions 415k
  • Answers 415k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use SvnClient.Export, passing in a SvnUriTarget constructed with the repository… May 15, 2026 at 8:51 am
  • Editorial Team
    Editorial Team added an answer This should work for you: counts = arange(0, len(list_of_dates)) plot(list_of_dates,… May 15, 2026 at 8:51 am
  • Editorial Team
    Editorial Team added an answer I've never changed the manifest data, but it might be… May 15, 2026 at 8:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.