了解 C# 中的逆变

本文关键字:了解 | 更新日期: 2023-09-27 18:30:51

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

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

了解 C# 中的逆变

泛型变量用于接口和德尔盖特

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

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

这里(逆变)关键字是告诉编译器 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()