Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Particulars about Action delegate in C#

Tags:

c#

delegates

1) What is the real definition for Action delegate? some definitions describe it is as polymorphic conditional map , some say it *Applied decision Table *.

(You may ask what will you achieve by knowing definition , if i know it i can understand its real purpose).

2) Thanks Binary Worrier,Andrew Hare of stackoverflow for giving nice examples. When i declare

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

i can understand what it actually does.But when i declare ,How does C# interpret 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 store collections of Actions in dictionary ?.unfortunately as the example was incomplete i did not get it.

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

like image 257
user193276 Avatar asked Oct 22 '09 06:10

user193276


3 Answers

It's just another delegate. Action<T> is declared like this:

void Action<T>(T item)

It's just "something which acts on a single item". There are generic overloads with more type parameters and normal parameters. In itself, an Action<T> isn't an applied decision table or anything like that - it's just a delegate which can do "something" with an item.

The dictionary example is just a dictionary with enum values as keys, and actions as values - so you can look up what to do based on the enum value, and then pass in a User reference for it to act on.

As for Func vs Action vs Predicate: Func is like Action, but returning a value. Predicate is similar, but always returns bool, and there aren't the range of generic overloads, just Predicate<T> to determine if an item "matches" the predicate.

like image 77
Jon Skeet Avatar answered Nov 15 '22 16:11

Jon Skeet


Action, Func and Predicate have different signatures:

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

Action<...> is the same as Func<..., void>
Predicate<T> is the same as Func<T, bool>

like image 45
user200783 Avatar answered Nov 15 '22 15:11

user200783


1) the Action delegates

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

are general purpose delegate to avoid the creation of to many delegate in your application. The idea is :

//- 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 dictionary stores for each 'SomeEnum' values, a method wicth match 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) Difference between Action , Function ,Predicate : - an action is a void method(no return value) - a function is a non void method (has a return value) - a predicate must return a boolean value and take 1 argument (it basically answere yes or no to question that take 1 argument)

I hope this help.

like image 27
Manitra Andriamitondra Avatar answered Nov 15 '22 16:11

Manitra Andriamitondra