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, IEnumerable

Type 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

NameDescription
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.cs

Initializes a new instance of the SynchronizedList<T> class that is empty.

public SynchronizedList()

SynchronizedList(IEnumerable<T>?)

SynchronizedList.cs

Initializes a new instance of the SynchronizedList<T> class that contains elements copied from the specified collection.

public SynchronizedList(IEnumerable<T>? items = null)
Parameters
items IEnumerable<T>?
The collection whose elements are copied to the new list.

SynchronizedList(int)

SynchronizedList.cs

Initializes a new instance of the SynchronizedList<T> class that is empty and has the specified initial capacity.

public SynchronizedList(int capacity)
Parameters
capacity int
The number of elements that the new list can initially store.

SynchronizedList(ReadOnlySpan<T>)

SynchronizedList.cs

Initializes a new instance of the SynchronizedList<T> class that contains elements copied from the specified span.

public SynchronizedList(ReadOnlySpan<T> items)
Parameters
items ReadOnlySpan<T>
The span whose elements are copied to the new list.

Properties

NameDescription
Count
System.Collections.Generic.ICollection<T>.IsReadOnly
System.Collections.ICollection.IsSynchronized
System.Collections.ICollection.SyncRoot
public int Count { get; }

Property value

int

No documentation is available.

System.Collections.Generic.ICollection<T>.IsReadOnly

SynchronizedList.cs
bool ICollection<T>.IsReadOnly { get; }

Property value

bool

No documentation is available.

System.Collections.ICollection.IsSynchronized

SynchronizedList.cs
bool ICollection.IsSynchronized { get; }

Property value

bool

No documentation is available.

System.Collections.ICollection.SyncRoot

SynchronizedList.cs
object ICollection.SyncRoot { get; }

Property value

object

No documentation is available.

Indexers

NameDescription
this[int]
public T this[int index] { get; set; }

Parameters

index int

Property value

T

No documentation is available.

Methods

NameDescription
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()
public void Add(T item)

Parameters

item T

No documentation is available.

public void Clear()

No documentation is available.

public bool Contains(T item)

Parameters

item T

Returns

bool

No documentation is available.

CopyTo(T[], int)

SynchronizedList.cs
public void CopyTo(T[] array, int arrayIndex)

Parameters

array T[]
arrayIndex int

No documentation is available.

EnsureCapacity(int)

SynchronizedList.cs

Ensures that the capacity of this list is at least the specified capacity.

public int EnsureCapacity(int capacity)

Parameters

capacity int
The minimum capacity to ensure.

Returns

int The new capacity of this list.

Execute overloads

Execute(Action<List<T>>)

SynchronizedList.cs

Runs the specified action on the underlying list while holding the lock, so the operations it performs are atomic with respect to other members.

public void Execute(Action<List<T>> action)
Parameters
action Action<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.cs

Runs the specified function on the underlying list while holding the lock, so the operations it performs are atomic with respect to other members.

public TResult Execute<TResult>(Func<List<T>, TResult> func)
Type parameters
TResult
The type of the value returned by func.
Parameters
func Func<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.cs

Returns an enumerator that iterates over a point-in-time snapshot of the list.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

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.

public int IndexOf(T item)

Parameters

item T

Returns

int

No documentation is available.

Insert(int, T)

SynchronizedList.cs
public void Insert(int index, T item)

Parameters

index int
item T

No documentation is available.

public bool Remove(T item)

Parameters

item T

Returns

bool

No documentation is available.

RemoveAt(int)

SynchronizedList.cs
public void RemoveAt(int index)

Parameters

index int

No documentation is available.

System.Collections.ICollection.CopyTo(Array, int)

SynchronizedList.cs
void ICollection.CopyTo(Array array, int index)

Parameters

array Array
index int

No documentation is available.

System.Collections.IEnumerable.GetEnumerator()

SynchronizedList.cs
IEnumerator IEnumerable.GetEnumerator()

No documentation is available.