c#4.0: int a real subtype of object? covariance, ienumerable and value types

前端 未结 4 1206
鱼传尺愫
鱼传尺愫 2021-01-02 09:18

I wonder why IEnumerable can\'t be assigned to a IEnumerable. After all IEnumerable is one of the few interfa
4条回答
  •  被撕碎了的回忆
    2021-01-02 09:45

    The simplistic answer is that this is just one of the quirks in the way that variance is implemented in C# and the CLR.

    From "Covariance and Contravariance in Generics":

    Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type.

提交回复
热议问题