Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between IAsyncEnumerable<T> vs IEnumerable<Task<T>>?

The new C# 8.0 and dotnet core 3 has this new feature of AsyncStreams (IAsyncEnumerable<T>). My understanding it that it provides a way to asynchronously process items in a stream. But would I not be able to do that with IEnumerable<Task<T>>?

what’s the difference between these two approaches?

like image 813
Bilal Fazlani Avatar asked Jul 20 '19 15:07

Bilal Fazlani


People also ask

What is task IEnumerable?

WhenAll(IEnumerable<Task>) Creates a task that will complete when all of the Task objects in an enumerable collection have completed. WhenAll(Task[]) Creates a task that will complete when all of the Task objects in an array have completed.

What is IAsyncEnumerable?

IAsyncEnumerable<T> InterfaceExposes an enumerator that provides asynchronous iteration over values of a specified type.

Whats the difference between async and task?

Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

Is ForEach Async in C#?

ForEach doesn't play particularly well with async (neither does LINQ-to-objects, for the same reasons). In this case, I recommend projecting each element into an asynchronous operation, and you can then (asynchronously) wait for them all to complete.


1 Answers

With a return type of IEnumerable<Task> you would return a blocking enumerator though the individual results are non-blocking.

With IAsyncEnumerable the enumerator itself is non-blocking, providing more flexibility for the source of the enumerable to go async.

like image 192
Frank Schwieterman Avatar answered Sep 18 '22 23:09

Frank Schwieterman