Back
Close

Your Ultimate async / await Tutorial in C#

AramT
141.5K views

There are some libraries in .Net Framework 4.5, that has a built-in support for asynchronous programming using asyncawait keywords.

These libraries, such as the HttpClient, StreamWriter, StreamReader and many more, provide you async methods that you can use in your program directly with await.

They have been added with the async/await capabilities because these provide functionalities that require long-running operations such as reading large file or download huge resources.

So the asyncawait will significantly improve the performance of your application while not blocking the UI or the execution.

I/O Bound async / await

Let's take an example for using the HttpClient's GetStringAsync method, that will hit a provided URL and return the html response content from it and display its length on Console screen:

Click run to see the console output
·using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace AsyncApp
{
class Program
{
private const string URL = "https://docs.microsoft.com/en-us/dotnet/csharp/csharp";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants