Table of Contents

Class SyncList<T>

Namespace
Utils.Common.Collections.Synchronized
Assembly
Utils.Common.dll

A list whose individual operations lock on SyncRoot, so single calls are safe from several threads. Compound operations (check-then-act, enumeration) are not atomic: lock on SyncRoot around them, or read Cache, an array snapshot that is rebuilt lazily after every modification.

public class SyncList<T> : BaseCachedCollection<T>

Type Parameters

T

The type of the items.

Inheritance
object
BaseCachedCollection<T>
SyncList<T>
Extension Methods

Constructors

SyncList()

Creates an empty list.

public SyncList()

SyncList(IEnumerable<T>)

Creates a list that contains the given items.

public SyncList(IEnumerable<T> items)

Parameters

items IEnumerable<T>

The items to copy into the list.

SyncList(int)

Creates an empty list with the given initial capacity.

Added in ATAS 8.0.15.

public SyncList(int count)

Parameters

count int

The initial capacity, not the number of items.

Properties

Count

Gets the number of items at the moment of the call.

public int Count { get; }

Property Value

int

IsReadOnly

Always false: the list can be modified.

public bool IsReadOnly { get; }

Property Value

bool

this[int]

Gets or sets the item at the given index.

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

Parameters

index int

The zero-based index of the item.

Property Value

T

Exceptions

ArgumentOutOfRangeException

index is outside the list.

Methods

Add(T)

Adds an item to the end of the list.

public void Add(T item)

Parameters

item T

The item to add.

Clear()

Removes all items.

public void Clear()

Contains(T)

Determines whether the list contains the item, using the default equality comparer.

public bool Contains(T item)

Parameters

item T

The item to locate.

Returns

bool

true if the item is found; otherwise, false.

CopyTo(T[], int)

Copies the items to an array, starting at the given index of the array.

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

Parameters

array T[]

The destination array.

arrayIndex int

The zero-based index in array at which copying begins.

Find(Predicate<T>)

Returns the first item that matches the predicate.

Added in ATAS 8.0.15.

public T? Find(Predicate<T> match)

Parameters

match Predicate<T>

The condition to search for.

Returns

T

The first matching item, or the default value of T if there is none.

GetCacheItems()

Builds the array returned by Cache: a copy of the current items.

protected override T[] GetCacheItems()

Returns

T[]

A new array with the items.

GetEnumerator()

Returns an enumerator over the underlying list. The lock is held only while the enumerator is created, not during the enumeration, so a concurrent modification can make the enumeration throw System.InvalidOperationException. Enumerate Cache instead when other threads may write.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator over the items.

IndexOf(T)

Returns the zero-based index of the first occurrence of the item.

public int IndexOf(T item)

Parameters

item T

The item to locate.

Returns

int

The index of the item, or -1 if it is not found.

Insert(int, T)

Inserts an item at the given index.

public void Insert(int index, T item)

Parameters

index int

The zero-based index at which the item is inserted.

item T

The item to insert.

Exceptions

ArgumentOutOfRangeException

index is less than 0 or greater than Count.

Remove(T)

Removes the first occurrence of the item.

public bool Remove(T item)

Parameters

item T

The item to remove.

Returns

bool

true if the item was found and removed; otherwise, false.

RemoveAll(Predicate<T>)

Removes all items that match the predicate. The predicate runs under the lock.

public void RemoveAll(Predicate<T> predicate)

Parameters

predicate Predicate<T>

The condition of the items to remove.

Exceptions

ArgumentNullException

predicate is null.

RemoveAt(int)

Removes the item at the given index.

public void RemoveAt(int index)

Parameters

index int

The zero-based index of the item to remove.

Exceptions

ArgumentOutOfRangeException

index is outside the list.

RemoveRange(int, int)

Removes a range of items.

Added in ATAS 8.0.15.

public void RemoveRange(int index, int count)

Parameters

index int

The zero-based index of the first item to remove.

count int

The number of items to remove.

Exceptions

ArgumentOutOfRangeException

index or count is less than 0.

ArgumentException

The range does not lie within the list.

Sort(IComparer<T>)

Sorts the items with the given comparer.

public void Sort(IComparer<T> comparer)

Parameters

comparer IComparer<T>

The comparer to use.

Exceptions

ArgumentNullException

comparer is null.

SyncGet<TParam1, TResult>(Func<List<T>, TParam1, TResult>, TParam1)

Runs a function against the underlying System.Collections.Generic.List<T> while holding the lock on SyncRoot, so several reads can be made atomically. The function must not keep the list or modify it: changes made here do not reset Cache.

Added in ATAS 8.0.15.

public TResult SyncGet<TParam1, TResult>(Func<List<T>, TParam1, TResult> action, TParam1 param1)

Parameters

action Func<List<T>, TParam1, TResult>

The function that receives the underlying list and param1.

param1 TParam1

The argument passed to the function, to avoid a capturing closure.

Returns

TResult

The value returned by the function.

Type Parameters

TParam1

The type of the argument passed to the function.

TResult

The type of the result.