What does an “in” generic parameter do?

后端 未结 2 438
春和景丽
春和景丽 2020-12-07 22:20

Saw this signature today:

public interface ISomeInterface

What impact does the in parameter have?

2条回答
  •  情书的邮戳
    2020-12-07 23:04

    That signifies generic contravariance. The opposite is covariance (keyword out).

    What this means is that when an interface is contravariant (in), then the interface can be implicitly converted to a generic type when the type parameter inherits T.

    Conversely for covariance out, the interface can be implicitly converted to a generic type where the type parameter is a 'lesser' type in the type hierarchy.

提交回复
热议问题