Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C#</p> <pre><code>myList&lt;string&gt;().ForEach( delegate(string name) { Console.WriteLine(name); }); </code></pre> <p>Anonymous delegates are not currently implemented in VB.Net, but both C# and VB.Net should be able to do lambdas:</p> <p>C#</p> <pre><code>myList&lt;string&gt;().ForEach(name =&gt; Console.WriteLine(name)); </code></pre> <p>VB.Net</p> <pre><code>myList(Of String)().ForEach(Function(name) Console.WriteLine(name)) </code></pre> <hr> <p>As Grauenwolf pointed out the above VB won't compile since the lambda doesn't return a value. A normal ForEach loop as others have suggested is probably the easiest for now, but as usual it takes a block of code to do what C# can do in one line.</p> <hr> <p>Here's a trite example of why this might be useful: this gives you the ability to pass in the loop logic from another scope than where the IEnumerable exists, so you don't even have to expose it if you don't want to.</p> <p>Say you have a list of relative url paths that you want to make absolute:</p> <pre><code>public IEnumerable&lt;String&gt; Paths(Func&lt;String&gt; formatter) { List&lt;String&gt; paths = new List&lt;String&gt;() { "/about", "/contact", "/services" }; return paths.ForEach(formatter); } </code></pre> <p>So then you could call the function this way:</p> <pre><code>var hostname = "myhost.com"; var formatter = f =&gt; String.Format("http://{0}{1}", hostname, f); IEnumerable&lt;String&gt; absolutePaths = Paths(formatter); </code></pre> <p>Giving you <code>"http://myhost.com/about", "http://myhost.com/contact"</code> etc. Obviously there are better ways to accomplish this in this specfic example, I'm just trying to demonstrate the basic principle.</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. 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