Covariance and Contravariance on the same type argument

后端 未结 7 696
时光取名叫无心
时光取名叫无心 2021-01-02 06:45

The C# spec states that an argument type cannot be both covariant and contravariant at the same time.

This is apparent when creating a covariant or contravariant int

7条回答
  •  -上瘾入骨i
    2021-01-02 07:11

    Covariance is possible for types you never input (e.g. member functions can use it as a return type or out parameter, but never as an input parameter). Contravariance is possible for types you never output (e.g. as an input parameter, but never as a return type or out parameter).

    If you made a type parameter both covariant and contravariant, you couldn't input it and you couldn't output it -- you couldn't use it at all.

提交回复
热议问题