Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Closures are simply great tools. When to use them? Any time you like... As has already been said, the alternative is to write a class; for example, pre C# 2.0, creating a parameterised thread was a real struggle. With C# 2.0 you don't even need the `ParameterizedThreadStart' you just do:</p> <pre><code>string name = // blah int value = // blah new Thread((ThreadStart)delegate { DoWork(name, value);}); // or inline if short </code></pre> <p>Compare that to creating a class with a name and value</p> <p>Or likewise with searching for a list (using a lambda this time):</p> <pre><code>Person person = list.Find(x=&gt;x.Age &gt; minAge &amp;&amp; x.Region == region); </code></pre> <p>Again - the alternative would be to write a class with two properties and a method:</p> <pre><code>internal sealed class PersonFinder { public PersonFinder(int minAge, string region) { this.minAge = minAge; this.region = region; } private readonly int minAge; private readonly string region; public bool IsMatch(Person person) { return person.Age &gt; minAge &amp;&amp; person.Region == region; } } ... Person person = list.Find(new PersonFinder(minAge,region).IsMatch); </code></pre> <p>This is <em>fairly</em> comparable to how the compiler does it under the bonnet (actually, it uses public read/write fields, not private readonly).</p> <p>The biggest caveat with C# captures is to watch the scope; for example:</p> <pre><code> for(int i = 0 ; i &lt; 10 ; i++) { ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(i); }); } </code></pre> <p>This might not print what you expect, since the <em>variable</em> i is used for each. You could see any combination of repeats - even 10 10's. You need to carefully scope captured variables in C#:</p> <pre><code> for(int i = 0 ; i &lt; 10 ; i++) { int j = i; ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(j); }); } </code></pre> <p>Here each j gets captured separately (i.e. a different compiler-generated class instance).</p> <p>Jon Skeet has a good blog entry covering C# and java closures <a href="http://msmvps.com/blogs/jon_skeet/archive/2008/05/06/the-beauty-of-closures.aspx" rel="noreferrer">here</a>; or for more detail, see his book <a href="http://www.manning.com/skeet/" rel="noreferrer">C# in Depth</a>, which has an entire chapter on them.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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