加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:19:14 所属栏目:百科 来源:网络整理
导读:任何人都可以解释,为什么以下代码产生错误? (在Microsoft Visual Studio 2008中编译) class Base1 { };class Base2 { }interface I1 { }interface I2 { }class C : I1,I2 { }static class Program{ static T M1T(this T t,I1 x) where T : Base1 { return t
任何人都可以解释,为什么以下代码产生错误? (在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”子句区分两种方法

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读