What does “out” mean before a Generic type parameter?

前端 未结 4 1991
我寻月下人不归
我寻月下人不归 2021-02-05 07:18

I\'ve just saw an unfamiliar syntax while looking for GroupBy return type:

public interface IGrouping : IEnumerable<         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 08:03

    It denotes a covariant parameter. See also the description on MSDN. Essentially it says, that IGrouping can be regarded as IGrouping, hence you can

    IGrouping gr = MakeGrouping(...);
    IGrouping grBase = gr;
    

    if Aderived is an interface or a type derived from Abase. This is a feature that comes in handy when you want to call a method that requires a parameter of type IGrouping, but you only got an object of type IGrouping. In this case, both types can be considered equivalent due to the covariance of their type parameters.

提交回复
热议问题