Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Async Loops: Await in foreach and for | Async Loops and Processing Collections
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Async and Await Practice

bookAsync Loops: Await in foreach and for

When working with collections of data in C#, you often need to perform operations on each item—such as downloading a list of web pages or processing files. Making these operations asynchronous can improve responsiveness and efficiency, but there are important details to understand when using async/await inside loops. A common pitfall is assuming that simply adding await inside a loop will make all operations run in parallel, when in fact, it usually causes them to run sequentially. Understanding the difference between sequential and parallel execution in async loops is key to writing efficient, correct code.

Note
Definition

sequential execution means each async operation completes before the next one starts.
parallel execution means multiple async operations are started and run at the same time, with their results awaited together.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; namespace ConsoleApp { public class Program { public static async Task Main(string[] args) { var urls = new List<string> { "https://example.com", "https://example.org", "https://example.net" }; await ProcessUrlsSequentially(urls); } public static async Task ProcessUrlsSequentially(List<string> urls) { using var httpClient = new HttpClient(); foreach (var url in urls) { Console.WriteLine($"Starting download: {url}"); var content = await httpClient.GetStringAsync(url); Console.WriteLine($"Downloaded {url}: {content.Length} characters"); } } } }

In this example, each URL is downloaded one after the other. The await inside the foreach loop means that the next iteration will not start until the current one has completed. This is called sequential execution. While this approach is simple and easy to understand, it can lead to slow performance if each operation takes a long time, because the total time will be roughly the sum of all individual operations.

To process multiple items concurrently, you can start all the asynchronous operations first and then await their completion together. This is done using Task.WhenAll and LINQ's Select method, as shown below.

ParallelProcessingSample.cs

ParallelProcessingSample.cs

copy
123456789101112131415161718192021
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; public class ParallelProcessingSample { public async Task ProcessUrlsInParallel(List<string> urls) { using var httpClient = new HttpClient(); var tasks = urls.Select(async url => { Console.WriteLine($"Starting download: {url}"); var content = await httpClient.GetStringAsync(url); Console.WriteLine($"Downloaded {url}: {content.Length} characters"); }); await Task.WhenAll(tasks); } }

When deciding between sequential and parallel processing, weigh the trade-offs. Use sequential processing when order matters or when each operation depends on the previous one. Choose parallel processing for independent operations, but be careful not to start too many tasks at once. Sometimes, using a limited degree of parallelism (such as batching) is the best approach.

1. What happens if you use await inside a foreach loop?

2. How can you process a collection in parallel using async/await?

3. What is a potential downside of running too many tasks in parallel?

question mark

What happens if you use await inside a foreach loop?

Select the correct answer

question mark

How can you process a collection in parallel using async/await?

Select the correct answer

question mark

What is a potential downside of running too many tasks in parallel?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookAsync Loops: Await in foreach and for

Svep för att visa menyn

When working with collections of data in C#, you often need to perform operations on each item—such as downloading a list of web pages or processing files. Making these operations asynchronous can improve responsiveness and efficiency, but there are important details to understand when using async/await inside loops. A common pitfall is assuming that simply adding await inside a loop will make all operations run in parallel, when in fact, it usually causes them to run sequentially. Understanding the difference between sequential and parallel execution in async loops is key to writing efficient, correct code.

Note
Definition

sequential execution means each async operation completes before the next one starts.
parallel execution means multiple async operations are started and run at the same time, with their results awaited together.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; namespace ConsoleApp { public class Program { public static async Task Main(string[] args) { var urls = new List<string> { "https://example.com", "https://example.org", "https://example.net" }; await ProcessUrlsSequentially(urls); } public static async Task ProcessUrlsSequentially(List<string> urls) { using var httpClient = new HttpClient(); foreach (var url in urls) { Console.WriteLine($"Starting download: {url}"); var content = await httpClient.GetStringAsync(url); Console.WriteLine($"Downloaded {url}: {content.Length} characters"); } } } }

In this example, each URL is downloaded one after the other. The await inside the foreach loop means that the next iteration will not start until the current one has completed. This is called sequential execution. While this approach is simple and easy to understand, it can lead to slow performance if each operation takes a long time, because the total time will be roughly the sum of all individual operations.

To process multiple items concurrently, you can start all the asynchronous operations first and then await their completion together. This is done using Task.WhenAll and LINQ's Select method, as shown below.

ParallelProcessingSample.cs

ParallelProcessingSample.cs

copy
123456789101112131415161718192021
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; public class ParallelProcessingSample { public async Task ProcessUrlsInParallel(List<string> urls) { using var httpClient = new HttpClient(); var tasks = urls.Select(async url => { Console.WriteLine($"Starting download: {url}"); var content = await httpClient.GetStringAsync(url); Console.WriteLine($"Downloaded {url}: {content.Length} characters"); }); await Task.WhenAll(tasks); } }

When deciding between sequential and parallel processing, weigh the trade-offs. Use sequential processing when order matters or when each operation depends on the previous one. Choose parallel processing for independent operations, but be careful not to start too many tasks at once. Sometimes, using a limited degree of parallelism (such as batching) is the best approach.

1. What happens if you use await inside a foreach loop?

2. How can you process a collection in parallel using async/await?

3. What is a potential downside of running too many tasks in parallel?

question mark

What happens if you use await inside a foreach loop?

Select the correct answer

question mark

How can you process a collection in parallel using async/await?

Select the correct answer

question mark

What is a potential downside of running too many tasks in parallel?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1
some-alt