user4951
user4951

Reputation: 33090

Are generic.list and generic.dictionary thread safe in .net

How do we know whether a method is thread safe or not

For example, if I check http://msdn.microsoft.com/en-us/library/3wcytfd1.aspx there is nothing that indicates its thread-safety.

Upvotes: 6

Views: 5628

Answers (2)

millimoose
millimoose

Reputation: 39960

The documentation for the entire List<T> class has a segment on thread safety:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Upvotes: 3

Mitch Wheat
Mitch Wheat

Reputation: 300579

No, they are not thread safe (without performing your own locking).

Use one of the Concurrent collections instead.

Thread-Safe Collections

The System.Collections.Concurrent namespace provides several thread-safe collection classes that should be used in place of the corresponding types in the System.Collections and System.Collections.Generic namespaces whenever multiple threads are accessing the collection concurrently.

Upvotes: 10

Related Questions