Problem understanding covariance contravariance with generics in C#

后端 未结 2 1903
梦如初夏
梦如初夏 2021-01-30 15:33

I can\'t understand why the following C# code doesn\'t compile.

As you can see, I have a static generic method Something with an IEnumerable parame

2条回答
  •  你的背包
    2021-01-30 16:18

    I just wanted to complement Eric's excellent insider answer with a code example for those that may not be that familiar with generic constraints.

    Change Something's signature like this: The class constraint has to come first.

    public static IList Something(IEnumerable foo) where T : class, IA
    

提交回复
热议问题