title: What's the VB.NET equivalent of async delegate in C#?
body: <p>I'm trying to convert the following extension method (<a href="https://devblogs.microsoft.com/pfxteam/implementing-a-simple-foreachasync-part-2/" rel="nofollow noreferrer">source</a>) from C# to VB:</p>
  
  <pre><code>public static Task ForEachAsync&lt;T&gt;(this IEnumerable&lt;T&gt; source,
                                     int dop, Func&lt;T, Task&gt; body)
  {
      return Task.WhenAll(
          from partition in Partitioner.Create(source).GetPartitions(dop)
          select Task.Run(async delegate {
              using (partition)
                  while (partition.MoveNext())
                      await body(partition.Current);
          }));
  }
  </code></pre>
  
  <p>The regular equivalent of <code>delegate</code> is <code>Sub()</code>, AFAIK, but I didn't expect it to work in this situation because of the <code>Async</code> keyword (and it didn't). So, I tried using <code>Function()</code> instead:</p>
  
  <pre class="lang-vb prettyprint-override"><code>&lt;System.Runtime.CompilerServices.Extension&gt;
  Public Function ForEachAsync(Of T)(source As IEnumerable(Of T),
                                     dop As Integer, body As Func(Of T, Task)) As Task
      Return Task.WhenAll(
          From partition In Partitioner.Create(source).GetPartitions(dop)
          Select Task.Run(Async Function() 'As Task '&lt;-- see below.
                              Using partition
                                  Do While partition.MoveNext()
                                      Await body(partition.Current)
                                  Loop
                              End Using
                          End Function))
  End Function
  </code></pre>
  
  <p>But this still doesn't compile and shows the following errors:</p>
  
  <ul>
  <li><p>At <code>WhenAll</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>Overload resolution failed because no accessible 'WhenAll' can be called with these arguments:
      'Public Shared Overloads Function WhenAll(Of TResult)(tasks As IEnumerable(Of Task(Of TResult))) As Task(Of TResult())': Type parameter 'TResult' cannot be inferred.
      'Public Shared Overloads Function WhenAll(Of TResult)(ParamArray tasks As Task(Of TResult)()) As Task(Of TResult())': Type parameter 'TResult' cannot be inferred.
  </code></pre></li>
  <li><p>At <code>Await body(partition.Current)</code>:</p>
  
  <blockquote>
    <p>'Await' may only be used in a query expression within the first
    collection expression of the initial 'From' clause or within the
    collection expression of a 'Join' clause.</p>
  </blockquote></li>
  <li><p>[Warning] at <code>Async Function()</code>: <sup>(it goes away if I add <code>As Task</code>)</sup></p>
  
  <blockquote>
    <p>Function '<code>&lt;anonymous method&gt;</code>' doesn't return a value on all code
    paths. A null reference exception could occur at run time when the
    result is used.</p>
  </blockquote></li>
  </ul>
  
  <p><strong>What am I doing wrong? And what is the correct way to do this in VB?</strong></p>