Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can multiple threads modify a dictionary?

In C#, can multiple threads read and write to a Dictionary provided each thread only accesses one element in the dictionary and never accesses another?

like image 516
Razor Avatar asked Feb 02 '26 06:02

Razor


1 Answers

No, a Dictionary is not Thread-safe.

With the exception of modifying the contents of a reference type (object) that is stored as the Value in a dictionary.

In .NET 4 we will have System.Collections.Concurrent.ConcurrentDictionary.

like image 157
Henk Holterman Avatar answered Feb 04 '26 23:02

Henk Holterman