List<T>.AddRange+ConcurrentDictionary issue

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;

static class Program
{
    static void Main()
    {
        ConcurrentDictionary<int, int> dictionary = new();

        for (int i = 1; i <= 50_000; i++)
            dictionary.TryAdd(i, default);

        List<KeyValuePair<int, int>> list = new();

        Thread thread = new(() =>
        {
            for (int i = -1; i >= -50_000; i--)
                dictionary.TryAdd(i, default);
        });
        thread.Start();

        list.AddRange(dictionary); // Throws

        thread.Join();
        Console.WriteLine($"dictionary.Count: {dictionary.Count:#,0}, list.Count: {list.Count:#,0}");
    }
}
;