c# 泛型的抗变和协变

namespace test
{

    // 泛型的协变,T 只能作为返回的参数
    public interface Class1<out T>
    {
        T Get();
        int Count { get; }
    }
    public class Class2 : Class1<String>
    {
        public String Get()
        {
            return "";
        }
        public int Count { get { return 1; } }
    }
    // 泛型的抗变,T在方法中只能作为传入参数
    public interface Class3<in T>
    {
        void Get(T t); // 作为参数
        // T GetT();//报错
    }
    public class Class4 : Class3<int>
    {
        public void Get(int age)
        {

        }
       
    }
}

猜你喜欢

转载自www.cnblogs.com/hnzheng/p/9113885.html