理解 C# 中的逆变

understanding contravariance in C#

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

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 逆变会修复它。但是,这按原样工作。让我想知道逆变在哪里适用?

泛型变体用于接口和委托

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

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     
    }
}

此处 out (Contravariant) 关键字是告诉编译器 B 的实例可以安全地被视为 b 的方法

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()