繁体   English   中英

为什么我会收到以下错误? 方差修饰符无效。 只能将接口和委托类型参数指定为变量

[英]Why do I get the following error? Invalid variance modifier. Only interface and delegate type parameters can be specified as variant

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

namespace Variance

{
  class A { }

  class B : A { }

  class C<out T>  { }

class Program
{
    static void Main(string[] args)
    {
        var v = new C<B>();

        CA(v);
    }

    static void CA(C<A> v) { }
  }
}

这是违规行:

class C<out T> 

正如错误消息所示,您不能将通用方差应用于类,仅应用于接口和委托。 这没关系:

interface C<out T>

以上不是。

有关详细信息,请参阅创建变体通用接口

您正在尝试将泛型方差应用于类。 这不受支持。 它仅在接口和委托类型上受支持。

非法:

class C<out T>  { }

法律:

interface C<out T> {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM