Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>List&lt;T&gt;.ForEach</code> is looking for an <code>Action&lt;T&gt;</code>. When you write</p> <pre><code>n.ForEach(Console.WriteLine); </code></pre> <p>what you have here is one of the members of the method group <code>Console.WriteLine</code> playing the role of an <code>Action&lt;T&gt;</code>. The compiler will look for the best overload of <code>Console.WriteLine</code> that eats instances of <code>int</code>. In fact, it will use the overload <a href="http://msdn.microsoft.com/en-us/library/70x4wcx1.aspx" rel="noreferrer"><code>Console.WriteLine(int)</code></a>. It will then use this overload to play the role of an <code>Action&lt;int&gt;</code>.</p> <p>For details on how this is done, see §6.6 of the specification (Method group conversions).</p> <p>However, when you write</p> <pre><code>n.ForEach(i =&gt; Console.WriteLine(i)); </code></pre> <p>we actually have a very different <code>Action&lt;int&gt;</code> In the first case, the <code>Action&lt;int&gt;</code> was <code>Console.WriteLine(int)</code>. Here, the <code>Action&lt;int&gt;</code> is equivalent to you having written</p> <pre><code>public static void DoSomething(int i) { Console.WriteLine(i); } </code></pre> <p>and then</p> <pre><code>n.ForEach(DoSomething); </code></pre> <p>(Of course, the compiler has to go through the same method group process as described above to figure out what is meant by <code>DoSomething</code>).</p> <p>The point is that in the first case the <code>Action&lt;int&gt;</code> <em>is</em> <code>Console.WriteLine(int)</code>. However, in the second case the <code>Action&lt;int&gt;</code> is a middle man (the lambda expression) that itself will call <code>Console.WriteLine(int)</code>.</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