IAsyncEnumerable: Think of It As an IAsyncIteratable


Starting in C# 8.0, C# shipped with new async enumerable features. The one we’re concerned with, for the intents/purposes/concerns of this blog post, is IAsyncEnumerable<T>.

The first thing to understand the differentiation between a standard collection (e.g.: IList<T>, IEnumerable<T>, or ICollection<T>) and an async enumerable (e.g.: IAsyncEnumerable<T>) is that an async enumerable isn’t enumerated as a collection until the collection is called/iterated through.

You can see this occur if you create an IAsyncEnumerable<T> but don’t iterate through it. As you can see below, everything inside of the IAsyncEnumerable<T> is null — even sources. This is because iteration hasn’t occurred, yet; and why would it, we’re not doing anything that causes enumeration.

Now, if we perform some task against the IAsyncEnumerable<T>, we’ll find that it enumerates and we can see the values derived at iteration during runtime.

For this reason, it’s important to think of IAsyncEnumerable<T> as a misnomer. It should be thought of, instead, as an IAsyncIteratable<T> — because the contents of the IAsyncEnumerable<T> aren’t evaluated until iteration is done (at runtime).


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.