I have the following method:
public async IEnumerable<string> GetListDriversAsync()
{
var drives = await graphClient.Drive.Root.Children.Request().GetAsync();
foreach (var d in drives)
yield return d.ToString();
}
But compiler error says:
"The return type of an async must be void, Task or Task
<T>
"
How do I return IEnumerable when the method is async?
Try this:
public async Task<IEnumerable<string>> GetListDriversAsync()
{
var drives = await graphClient.Drive.Root.Children.Request().GetAsync();
IEnumerable<string> GetListDrivers()
{
foreach (var d in drives)
yield return d.ToString();
}
return GetListDrivers();
}
An alternative way is possible in C# 8 . It uses IAsyncEnumerable .
public async IAsyncEnumerable<string> GetListDriversAsync()
{
var drives = await graphClient.Drive.Root.Children.Request().GetAsync();
foreach (var d in drives)
yield return d.ToString();
}
It will alter your signature a bit, which may (or may not) be an option for you.
Usage:
await foreach (var driver in foo.GetListDriversAsync())
{
Console.WriteLine(driver );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.