c# – 为什么这个电话不明确?

前端之家收集整理的这篇文章主要介绍了c# – 为什么这个电话不明确?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
任何人都可以解释,为什么以下代码产生错误? (在Microsoft Visual Studio 2008中编译)
  1. class Base1 { };
  2. class Base2 { }
  3.  
  4. interface I1 { }
  5. interface I2 { }
  6.  
  7. class C : I1,I2 { }
  8.  
  9. static class Program
  10. {
  11.  
  12. static T M1<T>(this T t,I1 x) where T : Base1
  13. {
  14. return t;
  15. }
  16.  
  17. static T M1<T>(this T t,I2 x) where T : Base2
  18. {
  19. return t;
  20. }
  21.  
  22. static void Main(string[] args)
  23. {
  24. Base1 b1 = new Base1();
  25. C c = new C();
  26. b1.M1(c);
  27. }
  28. }

错误

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)

我认为编译器可以使用“where”子句区分两种方法

解决方法

约束不是方法签名的一部分,因此不用于解决.

猜你在找的C#相关文章