Turning around with an example to highlight the problem:
public class BidirectionalMap<T1,T2> { public void Remove(T1 item) {} public void Remove(T2 item) {} public static void Test() {
So, the answer is that even if you cannot specify the restriction T1! = T2, it does not matter, because the compiler will fail as soon as you try to do something that violates the implicit restriction. It still catches a failure during compilation, so you can use these overloads with impunity. This is a bit strange since you can create a map instance (and even write IL code that manipulates the map accordingly), but the C # compiler will not allow you to damage the arbitrary resolution of ambiguous overloads.
The one-sided note is that such an overload can lead to some odd behavior if you are not careful. If you have a BidirectionalMap<Animal, Cat> and Cat: Animal, consider what happens with this code:
Animal animal = new Cat(); map.Remove(animal);
This will cause the overload that Animal accepts, so it will try to delete the key, even if you might have intended to delete the Cat value. This is a somewhat artificial case, but enough to warrant caution when very different behaviors occur as a result of method overloading. In such cases, it may be easier to read and maintain if you just give the methods different names that reflect their different behaviors (RemoveKey and RemoveValue, say.)
Dan bryant
source share