理解C#中的逆变

时间:2016-01-17 06:04:12

标签: c# contravariance

我正在学习逆变,并尝试以下方法来吸收这个概念:

interface B<T>
{
    T h();
}

public class SomeOtherClass<T> : B<T>
{
    public T h()
    {
        return default(T);
    }
}

public class Trial
{
    static void Main()
    {
        SomeOtherClass<Derived> h = new SomeOtherClass<Derived>();      
        Base b = h.h();     
    }
}

我原以为这个代码会在最后一个语句中出错,并认为让T逆变器会修复它。但是,这样可以正常工作。让我想知道逆变在哪里找到了适用性?

2 个答案:

答案 0 :(得分:2)

Generics varriance用于接口和delgates

将您的代码更改为以下内容,您将开始收到错误

public class Trial
{
    static void Main()
    {
        B<Derived> h = new SomeOtherClass<Derived>();      
        B<Base> b = h; // you will get compilation error until you add out keyword in interface B     
    }
}

这里(Contravariant)关键字是告诉编译器B的实例被认为是安全的方式b

答案 1 :(得分:0)

using System;
using static System.Console;
////
namespace ConsoleApplication1
{

    interface iContravariance<in T>
    {
        void Info(T  t);
    }

    class Master<T> : iContravariance<T>
    {

        public void Info(T insT)
        {

            if (insT is Parent) {
                WriteLine("--- As Parent: ---");
                WriteLine((insT as Parent).Method());
            }

            if (insT is Child) {
                WriteLine("--- As Child: ---") ;
                WriteLine((insT as Child).Method()); 
            }
        }
    }

    class Parent {

        public virtual String Method()
        {
            WriteLine("Parent Method()");
            return "";
        }

    }

    class Child : Parent    {

        public override String Method()
        {
            WriteLine("Child Method()");
            return "";
        }
    }

    class Client
    {
        static void Main(string[] args)
        {
            Child child      = new Child();
            Parent parent = new Parent();

            iContravariance<Parent> interP = new Master<Parent>();
            iContravariance<Child>   interC = interP;

            interC.Info(child);

            //interC.Info(parent); (It is compilation error.)

            ReadKey();
            return;
        }
    }
}

输出:

 --- As Parent: ---
 Child Method()
 --- As Child: ---
 Child Method()