C# generic method resolution fails with an ambiguous call error

前端 未结 2 1577
灰色年华
灰色年华 2021-02-13 22:09

Suppose I have defined two unrelated types and two extension methods with the same signature but different type filters:

public class Foo {}
public class Bar {}
         


        
2条回答
  •  再見小時候
    2021-02-13 22:22

    The constraint of a generic type parameter is not part of the method's signature. These two methods are essentially the same from a resolution point of view; when the compiler tries to resolve the call it sees two valid methods and it has no way to choose the better one, therefore the call is flagged as ambiguous.

    You can read more about this issue here.

提交回复
热议问题