tags:

views:

710

answers:

4

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?

+1  A: 

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>

Paul Baker
+5  A: 

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.

Jon Skeet
+1  A: 

Hi,

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.

Manitra Andriamitondra
Absoultely it is very helpful