Asynchronous streams
Asynchronous streams are the final missing piece in the task story that began several years ago when the Task
class, async
, and await
were first introduced. An example of an unresolved use case is the processing of data chunks coming from the internet while they are being downloaded. The basic point here is that we don't want to await the entire stream of data, but instead take a single chunk at a time, processing it and then awaiting the next one. This processing can therefore happen while the other pieces of data are still downloading and the unused thread time can be spent to serve other users as well, incrementing the total scalability of the application.
Before digging into the new C# feature, let's rapidly review how an enumerable is made in the synchronous world. The following examples show an enumerable sequence that can be used inside a foreach
statement; you may notice that the enumerated type is an integer instead of the hypothetical byte...