Features of the Action delegate in C #

1) What is the real definition for an Action delegate? some definitions describe it as a polymorphic conditional map , some say that it * is an applied decision table *.

(You may ask what you will achieve if you know the definition, if I know that I can understand its real purpose).

2) Thanks to Binary Worrier, Andrew Hare from stackoverflow for creating good examples. When i announce

string[] words = "This is as easy as it looks".Split(' '); `Array.ForEach(words, p => Console.WriteLine(p));` 

I can understand what he is actually doing. But when I declare how C # interprets, when i declare

  Dictionary<SomeEnum, Action<User>> methodList = new Dictionary<SomeEnum, Action<User>>() methodList.Add(SomeEnum.One, DoSomething); methodList.Add(SomeEnum.Two, DoSomethingElse); 

Does it save the Actions collection in the dictionary ?. Unfortunately, since the example was incomplete, I did not get it.

3) What is the functional difference between Action , Function ,Predicate delagets?

+6
c # delegates
source share
3 answers

This is just another delegate. Action<T> declared as follows:

 void Action<T>(T item) 

It is simply "something that acts on one element." There are general overloads with more typical parameters and normal parameters. The Action<T> itself is not an application decision table or something like this - it's just a delegate that can do something with the element.

An example dictionary is just a dictionary with enum values ​​as keys and actions as values, so you can see what to do based on the enumeration value, and then pass the User link to make it work.

Regarding Func vs Action vs Predicate : Func as Action , but returns a value. Predicate similar, but always returns bool , and there is no range of general overloads, just Predicate<T> to determine if the element matches the predicate.

+11
source share

Action, Func and Predicate have different signatures:

void Action <...> (...)
T Func <..., T> (...)
bool Predicate <T> (T)

The action <...> is the same as Func <..., void>
The predicate <T> is the same as Func <T, bool>

+2
source share

1) Action delegates

 (Action, Action<T>, Action<T, T2> ...) 

are general purpose delegates to avoid creating a large number of delegates in your application. The idea is this:

 //- Action => void method with 0 args //- Action<T> => void method with 1 arg of type T //- Action<T, T2> => void method with 2 args of type T et T2 //... 

2) that the dictionary stores for each value "SomeEnum", the method corresponds to this signature:

 void MethodExample (User arg); 

Here is an example:

 public Init() { deleteUserMethodsByStatus = new Dictionary<SomeEnum, Action<User>>(); deleteUserMethodsByStatus.Add(UserStatus.Active, user => { throw new BusinessException("Cannot delete an active user."); }); deleteUserMethodsByStatus.Add(UserStatus.InActive, DoUserDeletion}); } //This is how you could use this dictionary public void DeleteUser(int userId) { User u = DaoFactory.User.GetById(userId); deleteUserMethodsByStatus[u.Status](u); } //the actual deletion process protected internal DoUserDeletion(User u) { DaoFactory.User.Delete(u); } 

3) The difference between an action, function, predicate: - the action is a void method (without return value) - the function is a non-empty method (has a return value) - the predicate must return a logical value and take 1 argument (it basically answers yes or no to the question which takes one argument)

I hope this help.

+2
source share

All Articles