代码
using System;
// 协变
public interface IA<out T> where T:new() {
T fun();
//void fun(T t);//err
}
// 抗变
public interface IB<in T>{
void fun(T t);
//T fun();//err
}
class A:IA<C>{
public C fun(){
return new C();
}
}
class B:IB<C>{
public C c;
public void fun(C c){
this.c = c;
}
}
class C{
public int a = 5;
}
public class Fanxing{
public static void Main(){
Console.WriteLine("hello word");
A a = new A();
B b = new B();
C c1 = new C();
c1.a = 6;
b.fun(c1);
Console.WriteLine(a.fun().a);
Console.WriteLine(b.c.a);
}
}