What is the difference between SynchronizedCollection <T> and other concurrent collections?

How are the SynchronizedCollection<T> and the parallel collections in the System.Collections.Concurrent namespace different from each other, except for the parallel collections, which are the namespace, and the SynchronizedCollection<T> is a class?

SynchronizedCollection<T> , and all classes in parallel collections provide thread-safe collections. How can I decide when to use one over the other, and why?

+39
collections c # thread-safety concurrent-collections
Jan 11 '11 at 7:20
source share
1 answer

SynchronizedCollection<T> class was first introduced in .NET 2.0 to provide a thread-safe collection class. It does this with a lock, so you essentially have a List<T> where every access is enclosed in a lock statement.

The System.Collections.Concurrent namespace is much newer. It was not introduced before .NET 4.0 and includes a significantly improved and more diverse set of options. These classes no longer use locks to ensure thread safety, which means that they should scale better in situations where multiple threads access their data at the same time. However, the class that implements the IList<T> interface is especially absent among these parameters.

So, if you are targeting version 4.0 of the .NET Framework, you should use one of the collections provided by the System.Collections.Concurrent namespace when possible. As with choosing between the different types of collections presented in the System.Collections.Generic namespace, you need to choose the one whose functions and features best suit your specific needs.

If you are targeting an earlier version of the .NET Framework or you need a collection class that implements the IList<T> interface, you need to select the SynchronizedCollection<T> class.

This MSDN article also deserves attention: When to use assembly with threads

+49
Jan 11 2018-11-11T00:
source share



All Articles