-
Hi all, When I run this program several times on dotnet fiddle (net 6.0 and above): using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
IDictionary<string, string> dictionary = new ConcurrentDictionary<string, string>(); // FAILS sometimes
// ConcurrentDictionary<string, string> dictionary = new ConcurrentDictionary<string, string>(); // WORKS always
Parallel.For(0, 10, i => dictionary.TryAdd("x1", "y1"));
}
} I sometimes get But not casting to What's happening? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
With IDictionary you're likely invoking the TryAdd extension method which, unless it explicitly detects ConcurrentDictionary, likely isn't a threadsafe implementation. |
Beta Was this translation helpful? Give feedback.
With IDictionary you're likely invoking the TryAdd extension method which, unless it explicitly detects ConcurrentDictionary, likely isn't a threadsafe implementation.