簡體   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