This blog post is part of series of latest C# 8.0 features. In this post we will explore in detail about the new feature Nullable reference types.
In C# data structure IEnumerable plays an important role, language supports special syntax to create and use them. We have used foreach statement to iterate through the array in previous program, which was synchronous.
async and await were added to C# with version 5.0 to deal with asynchronous tasks to avoid blocking the thread. A program can asynchronously await and do other stuff until result it available or required to proceed.
Asynchronous streams combines both IEnumerables and C# together. Let's take a look at how it can be used with the help of below code snippet.
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using static
System.Console;
namespace AsynchronousStreams
{
class Program
{
static async Task
Main(string[] args)
{
await foreach (var
length in GetCharactersAsync())
{
WriteLine(length);
}
WriteLine("Press ENTER to exit...");
ReadLine();
}
private static async IAsyncEnumerable<int> GetCharactersAsync()
{
HttpClient client = new HttpClient();
string[] characters = {
"Daenerys
Targaryen", "Eddard
Stark", "Catelyn Stark", "Jon Snow", "Sansa Stark", "Arya Stark", "Robb Stark", "Jaime Lannister", "Cersei Lannister"
};
foreach (var character in characters)
{
var result = await
client.GetAsync($"https://www.google.com/search?q={character}");
var content = await
result.Content.ReadAsStringAsync();
yield return
content.Length;
}
}
}
}
Lets take a look in details at the snippet, in function GetCharacterAsync I have created an object of HttpClient and we have a list of GOT characters in array. We are iterating on array and searching on google asynchronously using GetAsync method of HttpClient. Since we don't have any other processing to do we are using await. Once we have result we will read the content asynchronously and return the length to the calling block. The foreach statement in Main function is iterating over the values and writing the length to console.
You might have noticed some unfamiliar syntax in above code snippet which makes this all possible. The GetCharactersAsync return type is IAsyncEnumerable<int> and async keyword adds an ability to asynchronous iteration.
private static async IAsyncEnumerable<int> GetCharactersAsync()
Another modification needs to make is in Main function where foreach statement is iterating over values returned from GetCharactersAsync function. The await keyword added before foreach statement insures the loop to awaits for the result before iterating to next value.
await foreach (var
length in GetCharactersAsync())
And the last one, since we are using await, compiler will show error await operator can only be used in async method. To resolve that we have added async in Main function declaration. Thanks to C# 7.2 for adding the support for async Main.
static async Task
Main(string[] args)
If you try to compile and run the program you may get lot of meaningless errors. This is because in preview version on .Net Core 3.0 and VS 2019 is not perfectly synchronized yet. Although as a workaround you need to add a boilerplate code in the project which can be found here.
Comments
Post a Comment