Extension methods with generics

后端 未结 1 1979
时光说笑
时光说笑 2021-01-15 02:31

I was looking at this question and i was curious why this deosn\'t compile.

Given this code, can anyone explain why the call to IBase.Test() doesn\'t re

相关标签:
1条回答
  • 2021-01-15 02:59

    Okay, the problem is that in the overload resolution process, the compiler finds all applicable candidate methods without checking the generic constraints specified in the method, picks the most specific one, and then checks the generic constraints.

    In this case, the generic method is more specific than the non-generic one (as after type parameter substitution, it's effectively a method with an IChildA parameter instead of an IBase parameter) - but it then fails the constraints.

    I have a blog post explaining this in more detail, and another one using it in a horrible way.

    0 讨论(0)
提交回复
热议问题