ToB企服应用市场:ToB评测及商务社交产业平台
标题:
C#委托与协变逆变
[打印本页]
作者:
一给
时间:
2022-8-9 14:43
标题:
C#委托与协变逆变
协变:子类向父类或基类转化
public class A { }
public class B : A { }
public class C : B { }
class Program
{
public static A T1() { return new A(); }
public static B T2(){ return new B();}
public static C T3() { return new C(); }
public delegate object testDeleg();
//public delegate A testDeleg();
static void Main(string[] args)
{
testDeleg testDeleg = T1;
testDeleg testDeleg1 = T2;
testDeleg testDeleg2 = T3;
}
}
复制代码
逆变:父类向子类转化
public class A { }
public class B : A { }
public class C : B { }
class Program
{
public static void T1(A a) { }
public static void T2(B b) { }
public static void T3(C c) { }
public delegate void TestDelegate(C c);
public static TestDelegate dele;
static void Main(string[] args)
{
dele += T1;
dele += T2;
dele += T3;
}
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4