Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action, Func and Predicate delegates - C# [duplicate]

I'm trying to understand the differences between the Action<T>, Func<T> and Predicate<T> delegates as part of my WPF/MVVM learning.

I know Action<T> and Func<T> both take zero to one+ parameters, only Func<T> returns a value while Action<T> don't.

As for Predicate<T> - I have no idea.

Therefore, I came up with this following questions:

  1. What does Predicate<T> do? (Examples welcomed!)
  2. If Action<T> returns nothing, wouldn't it be simpler to just use void instead? (Or any other type if it's Func<T> we're talking about.)

I'd like you to avoid LINQ/List examples in your questions.
I've seen those already but they just make it more confusing as the code that got me 'interested' in these delegates have nothing to do with it (I think!).
Therefore, I'd like to use a code I'm familiar with to get my answer.

Here it is:

public class RelayCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

public RelayCommand(Action<object> execute)
    : this(execute, null)
{
}

public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
    if (execute == null)
        throw new ArgumentNullException("execute");

    _execute = execute;
    _canExecute = canExecute;
}

[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
    return _canExecute == null ? true : _canExecute(parameters);
}

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameters)
{
    _execute(parameters);
}
}

Note:
I took out the comments to avoid super-long block of code.
The full code can be found HERE.

Any help is appreciated! Thanks! :)

P.S: Please don't point me to other questions. I did try to search but I couldn't find anything simple enough for me to understand.

like image 876
Asaf Avatar asked Jan 01 '13 13:01

Asaf


2 Answers

Predicate<T> is a delegate that takes a T and returns a bool.
It's completely equivalent to Func<T, bool>.

The difference is that Predicate<T> was added in .Net 2.0, whereas all of the Func<*> delegates were added in .Net 3.5. (except the ones with >8 parameters, which were added in .Net 4.0)

The LINQ-like methods in List<T> (FindAll(), TrueForAll(), etc) take Predicate<T>s.

To answer your second question, void cannot be used as a generic parameter.

like image 126
SLaks Avatar answered Sep 21 '22 20:09

SLaks


What does Predicate do? (Examples welcomed!)

Predicate is a function that takes an argument and returns bool e.g x > 20

If Action returns nothing, wouldn't it be simpler to just use void instead? (Or any other type if it's Func we're talking about.)

Action is defined as delegate that returns void. Here one may argue why there are two kind of delegates, but simply that's the outcome of the design. Another approach is to have Func that returns Unit that does nothing.

like image 28
Lukasz Madon Avatar answered Sep 24 '22 20:09

Lukasz Madon