What if you have a generic class that has methods that could be made ambiguous depending on the type arguments? I ran into this situation recently writing a two-way dictionary. I wanted to write symmetric Get()
methods that would return the opposite of whatever argument was passed. Something like this:
class TwoWayRelationship
{
public T2 Get(T1 key) { /* ... */ }
public T1 Get(T2 key) { /* ... */ }
}
All is well good if you make an instance where T1
and T2
are different types:
var r1 = new TwoWayRelationship();
r1.Get(1);
r1.Get("a");
But if T1
and T2
are the same (and probably if one was a subclass of another), it's a compiler error:
var r2 = new TwoWayRelationship();
r2.Get(1); // "The call is ambiguous..."
Interestingly, all other methods in the second case are still usable; it's only calls to the now-ambiguous method that causes a compiler error. Interesting case, if a little unlikely and obscure.