Note that there are some explanatory texts on larger screens.

plurals
  1. POAction, Func and Predicate delegates - C#
    primarykey
    data
    text
    <p>I'm trying to understand the differences between the <code>Action&lt;T&gt;, Func&lt;T&gt; and Predicate&lt;T&gt;</code> delegates as part of my WPF/MVVM learning.</p> <p>I know <code>Action&lt;T&gt; and Func&lt;T&gt;</code> both take zero to one+ parameters, only <code>Func&lt;T&gt;</code> returns a value while <code>Action&lt;T&gt;</code> don't.</p> <p>As for <code>Predicate&lt;T&gt;</code> - I have no idea.</p> <p>Therefore, I came up with this following questions: </p> <ol> <li>What does <code>Predicate&lt;T&gt;</code> do? (Examples welcomed!)</li> <li>If <code>Action&lt;T&gt;</code> returns <strong>nothing</strong>, wouldn't it be simpler to just use <code>void</code> instead? (Or any other type if it's <code>Func&lt;T&gt;</code> we're talking about.)</li> </ol> <p>I'd like you to avoid LINQ/List examples in your questions.<br> 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!).<br> Therefore, I'd like to use a code I'm familiar with to get my answer. </p> <p>Here it is: </p> <pre><code>public class RelayCommand : ICommand { readonly Action&lt;object&gt; _execute; readonly Predicate&lt;object&gt; _canExecute; public RelayCommand(Action&lt;object&gt; execute) : this(execute, null) { } public RelayCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; 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); } } </code></pre> <p><strong>Note:</strong><br> I took out the comments to avoid super-long block of code.<br> The full code can be found <a href="http://rachel53461.wordpress.com/2011/05/08/simplemvvmexample/" rel="nofollow">HERE</a>.</p> <p>Any help is appreciated! Thanks! :)</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload