SynchronizedList<T> Class
Represents a thread-safe list that can be accessed by multiple threads concurrently.
public sealed class SynchronizedList<T> : ICollection<T>, IEnumerable<T>, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection, IEnumerableType parameters
T- The type of elements in the list.
Remarks
Each member is individually atomic, but a sequence of members is not. For instance, if (!list.Contains(item)) list.Add(item); can add the same item twice, and list[list.Count - 1] can throw when another thread removes an element in between. Use SynchronizedList<T>.Execute or SynchronizedList<T>.Execute to run several operations as a single atomic unit.
Example
var list = new SynchronizedList<int>();
list.Add(1);
list.Add(2);
Assert.Equal([1, 2], list); // Enumerating the list enumerates a snapshot
// Multiple operations as a single atomic unit
list.Execute(items =>
{
if (!items.Contains(3))
{
items.Add(3);
}
});
Assert.Equal([1, 2, 3], list);- Implements
Constructors
| Name | Description |
|---|---|
| SynchronizedList() | Initializes a new instance of the SynchronizedList<T> class that is empty. |
| SynchronizedList(IEnumerable<T>?) | Initializes a new instance of the SynchronizedList<T> class that contains elements copied from the specified collection. |
| SynchronizedList(int) | Initializes a new instance of the SynchronizedList<T> class that is empty and has the specified initial capacity. |
| SynchronizedList(ReadOnlySpan<T>) | Initializes a new instance of the SynchronizedList<T> class that contains elements copied from the specified span. |
SynchronizedList overloads
SynchronizedList()
SynchronizedList.csInitializes a new instance of the SynchronizedList<T> class that is empty.
public SynchronizedList()SynchronizedList(IEnumerable<T>?)
SynchronizedList.csInitializes a new instance of the SynchronizedList<T> class that contains elements copied from the specified collection.
public SynchronizedList(IEnumerable<T>? items = null)Parameters
itemsIEnumerable<T>?- The collection whose elements are copied to the new list.
SynchronizedList(int)
SynchronizedList.csInitializes a new instance of the SynchronizedList<T> class that is empty and has the specified initial capacity.
public SynchronizedList(int capacity)Parameters
capacityint- The number of elements that the new list can initially store.
SynchronizedList(ReadOnlySpan<T>)
SynchronizedList.csInitializes a new instance of the SynchronizedList<T> class that contains elements copied from the specified span.
public SynchronizedList(ReadOnlySpan<T> items)Parameters
itemsReadOnlySpan<T>- The span whose elements are copied to the new list.
Properties
| Name | Description |
|---|---|
| Count | |
| System.Collections.Generic.ICollection<T>.IsReadOnly | |
| System.Collections.ICollection.IsSynchronized | |
| System.Collections.ICollection.SyncRoot |
Count
SynchronizedList.csSystem.Collections.Generic.ICollection<T>.IsReadOnly
SynchronizedList.csSystem.Collections.ICollection.IsSynchronized
SynchronizedList.csSystem.Collections.ICollection.SyncRoot
SynchronizedList.csIndexers
| Name | Description |
|---|---|
| this[int] |
this[int]
SynchronizedList.csMethods
| Name | Description |
|---|---|
| Add(T) | |
| Clear() | |
| Contains(T) | |
| CopyTo(T[], int) | |
| EnsureCapacity(int) | Ensures that the capacity of this list is at least the specified capacity. |
| Execute(Action<List<T>>) | Runs the specified action on the underlying list while holding the lock, so the operations it performs are atomic with respect to other members. |
| Execute<TResult>(Func<List<T>, TResult>) | Runs the specified function on the underlying list while holding the lock, so the operations it performs are atomic with respect to other members. |
| GetEnumerator() | Returns an enumerator that iterates over a point-in-time snapshot of the list. |
| IndexOf(T) | |
| Insert(int, T) | |
| Remove(T) | |
| RemoveAt(int) | |
| System.Collections.ICollection.CopyTo(Array, int) | |
| System.Collections.IEnumerable.GetEnumerator() |
Add(T)
SynchronizedList.csClear()
SynchronizedList.cspublic void Clear()No documentation is available.
Contains(T)
SynchronizedList.csCopyTo(T[], int)
SynchronizedList.csEnsureCapacity(int)
SynchronizedList.csExecute overloads
Execute(Action<List<T>>)
SynchronizedList.csRuns the specified action on the underlying list while holding the lock, so the operations it performs are atomic with respect to other members.
Parameters
actionAction<List<T>>- The action to execute. The list passed to the action must not be used after the action returns.
Remarks
Keep the action short and avoid calling code that may block or acquire other locks, because all other members block while it runs.
Execute<TResult>(Func<List<T>, TResult>)
SynchronizedList.csRuns the specified function on the underlying list while holding the lock, so the operations it performs are atomic with respect to other members.
Type parameters
TResult- The type of the value returned by
func.
Parameters
funcFunc<List<T>, TResult>- The function to execute. The list passed to the function must not be used after the function returns.
Returns
TResult The value returned by func.
Remarks
Keep the function short and avoid calling code that may block or acquire other locks, because all other members block while it runs.
GetEnumerator()
SynchronizedList.csReturns an enumerator that iterates over a point-in-time snapshot of the list.
public IEnumerator<T> GetEnumerator()Returns
Remarks
A full copy of the list is taken on each call so enumeration is safe while other threads mutate the list. Avoid enumerating in hot paths where the allocation matters.