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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:03:21+00:00 2026-06-15T11:03:21+00:00

I recently wrote this and was surprised that it compiles: public class MyGeneric<U, V>

  • 0

I recently wrote this and was surprised that it compiles:

public class MyGeneric<U, V> {
  MyGeneric(U u) { ... }
  MyGeneric(V v) { ... }
  public void Add(U u, V v) { ... }
  public void Add(V v, U u) { ... }
}

If I use this class as follows, I get an “Ambiguous constructor reference” and an “Ambiguous invocation” if I call Add.

var myVar = new MyGeneric<int, int>(new MyIntComparer());

Obviously, there’s no ambiguity when I use int and double as generic types, except of course when I use both ints, which would also both assign to a double.

var myVar = new MyGeneric<int, double>(new MyIntComparer());
myVar.Add(3, 5);

So then I thought that the following was also allowed, but surprisingly I got an error. Why is the following not allowed to compile?

public interface IMyInterface<T, S> {
  void Add(T t, S s);
}

public class MyGeneric<U, V> : IMyInterface<U, V>, IMyInterface<V, U> {
  public MyGeneric(U u) { }
  public MyGeneric(V v) { }
  void IMyInterface<U, V>.Add(U u, V v) { ... }
  void IMyInterface<V, U>.Add(V v, U u) { ... }
}

Regardless if I use implicit or explicit interface implementation, the compiler states that

‘MyGeneric<U,V>’ cannot implement both ‘IMyInterface<U,V>’ and ‘IMyInterface<V,U>’ because they may unify for some type parameter substitutions

And why is the first allowed to write?

  • 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-15T11:03:22+00:00Added an answer on June 15, 2026 at 11:03 am

    1- Why is the following not allowed to compile ?

    Part of the response is in that post : Why does the C# compiler complain that "types may unify" when they derive from different base classes?

    The section 13.4.2 of the C# 4 specification states:

    The interfaces implemented by a generic type declaration must remain
    unique for all possible constructed types. Without this rule, it would
    be impossible to determine the correct method to call for certain
    constructed types.

    2- And why is the first allowed to write?

    The compiler perform the generic type check at compile time, the section 7.4.3.5 of the C# 4 specification states:

    While signatures as declared must be unique, it is possible that
    substitution of type arguments results in identical signatures. In
    such cases, the tie-breaking rules of overload resolution above will
    pick the most specific member. The following examples show overloads
    that are valid and invalid according to this rule:

    interface I1<T> {...}
    interface I2<T> {...}
    class G1<U>
    {
        int F1(U u);                    // Overload resulotion for G<int>.F1
        int F1(int i);                  // will pick non-generic
        void F2(I1<U> a);               // Valid overload
        void F2(I2<U> a);
    }
    class G2<U,V>
    {
        void F3(U u, V v);          // Valid, but overload resolution for
        void F3(V v, U u);          // G2<int,int>.F3 will fail
        void F4(U u, I1<V> v);      // Valid, but overload resolution for   
       void F4(I1<V> v, U u);       // G2<I1<int>,int>.F4 will fail
        void F5(U u1, I1<V> v2);    // Valid overload
        void F5(V v1, U u2);
        void F6(ref U u);               // valid overload
        void F6(out V v);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote some data access methods (plain old Java) that use immutable objects
I recently wrote a theme function to add a class to my primary links
So I recently wrote a java class that extends exception and used an instance
I recently wrote a DLL in C# (.Net 2.0) which contains a class that
I recently wrote a class that renders B-spline curves. These curves are defined by
I've recently wrote a little script that get's my Twitter feed and caches it
I recently wrote a piece of code that looked a bit like this: IEnumerable<DTO.Employee>
I recently wrote a class library that includes some objects that model certain types
I recently found out about C# extension methods and wrote this one: /// <summary>
I recently wrote code that didnt work as i would expect, it was: message

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.