Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "out" mean before a Generic type parameter?

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

public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>

MSDN Source

I know what does out mean in methods, but not in a generics interface.

What does out mean in a generic type?

like image 994
gdoron is supporting Monica Avatar asked Apr 05 '12 11:04

gdoron is supporting Monica


People also ask

What is a generic type parameter?

Generic Methods A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

What is out and in in Kotlin?

"Out" keyword is extensively used in Kotlin generics. Its signature looks like this − List<out T> When a type parameter T of a class C is declared out, then C can safely be a super type of C<Derived>. That means, a Number type List can contain double, integer type list.

How do you indicate that a class has a generic type parameter?

A generic type is declared by specifying a type parameter in an angle brackets after a type name, e.g. TypeName<T> where T is a type parameter.

What is a formal type parameter?

Formal type parameters used in the class declaration have the same purpose as the formal parameters used in the method declaration. A class can use formal type parameters to receive type information when an instance is created for that class. The actual types used during instantiation are called actual type parameters.


4 Answers

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

IGrouping<Aderived, Bderived> gr = MakeGrouping(...);
IGrouping<Abase, Bbase> 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<Abase, Bbase>, but you only got an object of type IGrouping<Aderived, Bderived>. In this case, both types can be considered equivalent due to the covariance of their type parameters.

like image 79
Frank Avatar answered Oct 19 '22 08:10

Frank


It is one of the two generic modifiers introduces in C# 4.0 (Visual Studio 2010).

It signifies that the generic parameter it is declared on is covariant.

The in modifier signifies the generic parameter it is declared on is contravariant.

See out (Generic Modifier) and in (Generic Modifier) on MSDN.

like image 44
Oded Avatar answered Oct 19 '22 08:10

Oded


out just means that the type is only used for output e.g.

public interface Foo<out T>
{
   T Bar()
}

There also is a modifier in that means that the type is only used for input e.g.

public interface Foo<in T>
{
    int Bar(T x)
}

These are used because Interfaces with in are covariant in T and Interfaces with out are contravariant in T.

like image 21
Ral Zarek Avatar answered Oct 19 '22 09:10

Ral Zarek


out keyword in this context would indicate the corresponding type parameter to be covariant simply speaking - covariance enables you to use a more derived type than that specified by the generic parameter.

BTW, see this ten part series from Eric Lippert to understand more about covariance and contra-variance: http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.aspx

like image 36
VinayC Avatar answered Oct 19 '22 08:10

VinayC