

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are Async Streams in C# 8.0?
C# 8.0 introduces async streams, which model a streaming source of data. Data streams often retrieve or generate elements asynchronously.
The code that generates the sequence can now use yield return to return elements in a method that was declared with the async modifier.
We can consume an async stream using an await foreach loop.
This below Syntax
static IEnumerable<string> Message(){ yield return "Hello!"; yield return "Hello!"; } Can be replaced by IAsyncEnumerable static async IAsyncEnumerable<string> MessageAsync(){ await Task.Delay(2000); yield return "Hello!"; await Task.Delay(2000); yield return "Hello!"; }
Example
class Program{ public static async Task Main(){ await foreach (var item in MessageAsync()){ System.Console.WriteLine(item); } Console.ReadLine(); } static async IAsyncEnumerable<string> MessageAsync(){ await Task.Delay(2000); yield return "Hello!"; await Task.Delay(2000); yield return "Hello!"; } }
Output
Hello! Hello!
- Related Questions & Answers
- Streams on Arrays in Java 8
- Java 8 Streams and its operations
- What are async methods in JavaScript?
- What are async generator methods in JavaScript?
- Difference between Streams and Collections in Java 8
- What are the methodologies of data streams clustering?
- What are cin, cout and cerr streams in C++?
- Streams and Byte Streams in C#
- What are Default Methods in Java 8?
- What are the core interfaces of Reactive Streams in Java 9?
- What is the necessity of byte streams and character streams in Java?
- Streams In C#
- Streams in Java
- Sync vs Async vs Async/Await in fs-extra - NodeJS
- What is a Stream and what are the types of Streams and classes in Java?
Advertisements