How to use asynchronous streams in C# 8.0

Take advantage of the ability to create and consume data streams asynchronously in C# 8.0 to improve the performance of your applications

Asynchronous programming has been around for quite a while now. The introduction of the async and await keywords in .NET enabled us to write programs that could take advantage of asynchrony with ease. However, there hasn’t been any way to consume streams of data asynchronously until the arrival of IAsyncEnumerable<T> in C# 8.0.

IAsyncEnumerable<T> is similar to the IEnumerable<T> method used to iterate over a collection, except that IAsyncEnumerable<T> allows us to move through the collection asynchronously. In other words, IAsyncEnumerable<T> allows us to wait for the next element in the collection without blocking a thread.

In this article we’ll take a look at the challenges that IAsyncEnumerable<T> solves, with code examples wherever relevant. To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here

Create a console application project in Visual Studio 2019

First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio 2019.

To continue reading this article register now