Generic extension method resolution fails

前端 未结 3 1705
醉梦人生
醉梦人生 2020-12-06 13:47

The following program does not compile, because in the line with the error, the compiler chooses the method with a single T parameter as the resolution, which f

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 14:13

    1. While I know you dont want it, I think you should really re-think if method names should be the same. I cannot see how the same name can act on an instance, and collection of such instances. For eg, if your method name is Shoot for T, then the other method should sound like ShootThemAll or something similar.

    2. Or else you should make your assignment slightly different:

      IEnumerable instances = new List();
      instances.Method(); //now this should work
      
    3. As a last option, as Dimitry says in comments you have to explicitly specify the type argument.

      instances.Method();
      

提交回复
热议问题