Why is this challenge ambiguous?

Can someone explain why the following code is causing an error? (Compilation in Microsoft Visual Studio 2008)

class Base1 { }; class Base2 { } interface I1 { } interface I2 { } class C : I1, I2 { } static class Program { static T M1<T>(this T t, I1 x) where T : Base1 { return t; } static T M1<T>(this T t, I2 x) where T : Base2 { return t; } static void Main(string[] args) { Base1 b1 = new Base1(); C c = new C(); b1.M1(c); } } 

error

The call is ambiguous between the following methods or properties: ' ConsoleApplication1.Program.M1<ConsoleApplication1.Base1>(ConsoleApplication1.Base1, ConsoleApplication1.I1) ' and ' ConsoleApplication1.Program.M1<ConsoleApplication1.Base1>(ConsoleApplication1.Base1, ConsoleApplication1.I2) ''

I thought the compiler could distinguish between two methods using the where clauses

+6
source share
3 answers

Constraints are not part of the signature for methods and therefore are not used for resolution.

+13
source

Limitations are not part of the signature. See Eric Lippert's related article for more details.

+3
source

The restriction cannot be used to allow communication.

0
source

Source: https://habr.com/ru/post/903372/


All Articles