Post

C# List<T> Add Method Missing Items When Count Inspected When Adding Items In An Async Task

C# List<T> Add Method Missing Items When Count Inspected When Adding Items In An Async Task

Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class Program
{
    public static async Task Main()
    {
        var items = new List<string>(); // Does not consistently write 100 to console on my machine
        // Potential fix, if ConcurrentBag<T> works for your use case:
        // var items = new ConcurrentBag<string>();

        var tasks = Enumerable.Repeat("a", 100)
            .Select(async x =>
            {
                await Task.Delay(1);
                items.Add(x);
            });

        await Task.WhenAll(tasks);

        Console.WriteLine(items.Count);
    }
}

What’s happening?

items.Count may not consistently be 100. List<T> is not thread-safe. We can replace it with ConcurrentBag<T>, provided a collection of thread-safe unordered objects works for your use case.

See https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent for other potential options.

This post is licensed under CC BY 4.0 by the author.