Cannot cast between generic types

后端 未结 4 1445
清歌不尽
清歌不尽 2021-01-14 21:54

I\'m trying to do this:

interface IA
{
}

class A : IA
{
}

class Foo where T: IA
{
}

class Program
{
    static void Main( string[] args )
    {
          


        
4条回答
  •  逝去的感伤
    2021-01-14 22:42

    All generic classes are invariant. Interfaces (and delegates) on the other hand can support co- and contra-variance, but only in the cases where it's possible safely. And they need to opt-in explicitly.

    For example via IFoo or IFoo

提交回复
热议问题