Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action<T> or Action<in T>?

I was reading about Action Delegate on MSDN and so this under syntax

 public delegate void Action<in T>(T obj);

Than I looked in c-sharpcorner.com and it used this syntax

public delegate void Action<T>(T obj);   

As you can see there is no in before T.
Which syntax is right and what does that in mean?
EDIT: The same syntax used for Predicate.

Thanks.

like image 987
Samvel Siradeghyan Avatar asked Nov 02 '10 06:11

Samvel Siradeghyan


People also ask

What is the difference between action t and FUNC T >?

Actions can only take input parameters, while Funcs can take input and output parameters. It is the biggest difference between them. An action can not return a value, while a func should return a value in the other words.

What is action t used for in an application?

Action<T> Delegate (System)Encapsulates a method that has a single parameter and does not return a value.

What is action in C sharp?

Action is a delegate type defined in the System namespace. An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. For example, the following delegate prints an int value.

Is a predicate and action?

In English, a complete sentence or clause requires two parts: an action and the person or thing that's performing the action. While the subject describes who is doing the action, the predicate describes the action itself. Along with subjects, predicates are a necessary part of English sentence structure.


2 Answers

in and out (generic contravariance and covariance) were only introduced in C# 4, and the delegates and interfaces were modified for .NET 4 - so Action<T> in .NET 3.5 became Action<in T> in .NET 4.

The article you're referring to is from 2006, well before .NET 4 came out.

If you change which version of MSDN you're displaying, you'll see the change - for example, the .NET 3.5 version shows it without the in.

like image 155
Jon Skeet Avatar answered Oct 02 '22 22:10

Jon Skeet


The in part is for covariance and contravariance and was introduced in .NET 4.0, the article you link to was published in 2006 before .NET 4.0 was released (so obviously it doesn't refer co[ntra]variance).

like image 45
Motti Avatar answered Oct 03 '22 00:10

Motti