微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

任何人都可以解释,为什么以下代码产生错误? (在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);
    }
}

错误

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”子句区分两种方法

解决方法

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

原文地址:https://www.jb51.cc/csharp/98163.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐