ToListAsync extension method for IAsyncEnumerable<T> #3935
-
The In a similar sense that I like to think others may find this useful if built into the C# library, especially since I have created and have been using the following extension method to suit my needs: public static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> items)
{
var evaluatedItems = new List<T>();
await foreach (var item in items)
evaluatedItems.Add(item);
return evaluatedItems;
} Example on usage, with a method doing something like this: public async IAsyncEnumerable<string> ReadAsync(string path)
{
await using (var fileStream = File.OpenRead(path))
using (var streamReader = new StreamReader(fileStream))
{
yield return await streamReader.ReadLineAsync();
}
} it could be used either way (similar to what is already possible with public async Task DoStuffLineByLineAsync()
{
await foreach(var line in ReadAsync("some file path"))
ProcessLine(line); // process each line
}
public async Task DoStuffAtOnceAsync()
{
List<string> allLines = await ReadAsync("some file path").ToListAsync();
CalculateSomething(allLines); // needed all the lines before processing
} Any thoughts on adding this, and possibly other |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 16 replies
-
I would not be surprised to see an
|
Beta Was this translation helpful? Give feedback.
-
I'd suggest opening a discussion at the Runtime repository as they handle API discussions. This repository is more for evolution of the C# language itself. |
Beta Was this translation helpful? Give feedback.
-
Check out System.Linq.Async by ReactiveExtensions: https://www.nuget.org/packages/System.Linq.Async |
Beta Was this translation helpful? Give feedback.
-
For whatever reason, the BCL team is dead-set against including any extension methods for IAsyncEnumerable, instead delegating it all to Rx (which of course has no where near the level of support as the BCL) |
Beta Was this translation helpful? Give feedback.
-
That said, if it's a question of visibility and discoverability that said MS NuGet packages exist, which team would that fall upon? Like say if a developer tried to do |
Beta Was this translation helpful? Give feedback.
I'd suggest opening a discussion at the Runtime repository as they handle API discussions. This repository is more for evolution of the C# language itself.