What goes in and out on a delegate in C #?

In C # code, I found this tool.

I tried to find out what kind of meaning and meaning, but only an explanation of the keyword.

So what are these in and out keywords?

public delegate Tb Reader<in Ta, out Tb>( Ta a );
+4
source share
2 answers

The parameter inindicates that the type parameter is contravariant - you can pass in a class that inherits Ta.

The parameter outindicates that the parameter is covariant -> you can use more derived types.

See here for in modifer and here for out modifier.

+3
source

They make it possible to make an example below.

Reader<string,object> first = someString => return someObject;
Reader<object,string> second= someObject => return someString;
first=second;
-1
source

Source: https://habr.com/ru/post/1684181/


All Articles