[英]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) { }
}
}
This is the offending line: 这是违规行:
class C<out T>
As the error message tells you, you can't apply generic variance to classes, only to interfaces and delegates. 正如错误消息所示,您不能将通用方差应用于类,仅应用于接口和委托。 This would be okay: 这没关系:
interface C<out T>
The above is not. 以上不是。
For details, see Creating Variant Generic Interfaces 有关详细信息,请参阅创建变体通用接口
You're attempting to apply generic variance to a class. 您正在尝试将泛型方差应用于类。 This is not supported. 这不受支持。 It is only supported on interfaces and delegate types. 它仅在接口和委托类型上受支持。
Illegal: 非法:
class C<out T> { }
Legal: 法律:
interface C<out T> {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.