Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The difference between <code>Func</code> and <code>Action</code> is simply whether you want the delegate to return a value (use <code>Func</code>) or not (use <code>Action</code>).</p> <p><code>Func</code> is probably most commonly used in LINQ - for example in projections:</p> <pre><code> list.Select(x =&gt; x.SomeProperty) </code></pre> <p>or filtering:</p> <pre><code> list.Where(x =&gt; x.SomeValue == someOtherValue) </code></pre> <p>or key selection:</p> <pre><code> list.Join(otherList, x =&gt; x.FirstKey, y =&gt; y.SecondKey, ...) </code></pre> <p><code>Action</code> is more commonly used for things like <code>List&lt;T&gt;.ForEach</code>: execute the given action for each item in the list. I use this less often than <code>Func</code>, although I <em>do</em> sometimes use the parameterless version for things like <code>Control.BeginInvoke</code> and <code>Dispatcher.BeginInvoke</code>.</p> <p><code>Predicate</code> is just a special cased <code>Func&lt;T, bool&gt;</code> really, introduced before all of the <code>Func</code> and most of the <code>Action</code> delegates came along. I suspect that if we'd already had <code>Func</code> and <code>Action</code> in their various guises, <code>Predicate</code> wouldn't have been introduced... although it <em>does</em> impart a certain meaning to the use of the delegate, whereas <code>Func</code> and <code>Action</code> are used for widely disparate purposes.</p> <p><code>Predicate</code> is mostly used in <code>List&lt;T&gt;</code> for methods like <code>FindAll</code> and <code>RemoveAll</code>.</p>
 

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