vb.net 等同于 c# 中的 await foreach

vb.net equivalent to await foreach in c#

在 C# 中发现 VB.net 等同于 await foreach 的是什么?

根据我发现的 VB.net 的 For Each 不能使用 Await 运算符 (Await Operator (Visual Basic))

例如,如何将此 C# 示例转换为 VB.net (List blobs with Azure Storage client libraries)?

private static async Task ListBlobsFlatListing(BlobContainerClient blobContainerClient, 
                                               int? segmentSize)
{
    try
    {
        // Call the listing operation and return pages of the specified size.
        var resultSegment = blobContainerClient.GetBlobsAsync()
            .AsPages(default, segmentSize);

        // Enumerate the blobs returned for each page.
        await foreach (Azure.Page<BlobItem> blobPage in resultSegment)
        {
            foreach (BlobItem blobItem in blobPage.Values)
            {
                Console.WriteLine("Blob name: {0}", blobItem.Name);
            }

            Console.WriteLine();
        }
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}

您可能需要编写 For Each 变成的等效代码。对于常规(同步)For Each 循环:

For Each item In Sequence
    'Do whatever with item
Next

相当于:

Dim iterator = Sequence.GetEnumerator()

Do While iterator.MoveNext()
    Dim item = iterator.Current
    'Do whatever with item
End Do

我希望异步可枚举的转换基本相同。

Dim iterator As IAsyncEnumerator(Of Object) 'Or appropriate type

Try
    iterator = AsyncSequence.GetAsyncEnumerator()
    Do While Await iterator.MoveNextAsync()
        Dim item = iterator.Current
        'Do whatever with item
    End Do
Finally
    Await iterator.DisposeAsync()
End Try

它不像您可以编写 Await For Each 那样干净(并且有适当的 Using 支持),但也相差不远。