Sim*_*mie 4 vb.net c#-to-vb.net
VB.net 相当于await foreachC# 中的什么?
根据我发现 VB.netFor Each无法使用Await运算符(Await Operator (Visual Basic))
例如,如何将此 C# 示例转换为 VB.net(使用 Azure 存储客户端库列出 blob)?
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;
}
}
Run Code Online (Sandbox Code Playgroud)
您可能需要编写 aFor Each变成的等效代码。对于传统(同步)For Each循环:
For Each item In Sequence
'Do whatever with item
Next
Run Code Online (Sandbox Code Playgroud)
相当于:
Dim iterator = Sequence.GetEnumerator()
Do While iterator.MoveNext()
Dim item = iterator.Current
'Do whatever with item
End Do
Run Code Online (Sandbox Code Playgroud)
我希望异步可枚举的转换本质上是相同的。
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
Run Code Online (Sandbox Code Playgroud)
它并不像您可以编写Await For Each(并有适当的Using支持)那么干净,但也相差不远。
| 归档时间: |
|
| 查看次数: |
707 次 |
| 最近记录: |