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
TThe type of the items.
- Inheritance
-
objectBaseCachedCollection<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
itemsIEnumerable<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
countintThe 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
indexintThe zero-based index of the item.
Property Value
- T
Exceptions
- ArgumentOutOfRangeException
indexis outside the list.
Methods
Add(T)
Adds an item to the end of the list.
public void Add(T item)
Parameters
itemTThe 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
itemTThe 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
arrayT[]The destination array.
arrayIndexintThe zero-based index in
arrayat 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
matchPredicate<T>The condition to search for.
Returns
- T
The first matching item, or the default value of
Tif 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
itemTThe 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
indexintThe zero-based index at which the item is inserted.
itemTThe item to insert.
Exceptions
- ArgumentOutOfRangeException
indexis less than 0 or greater than Count.
Remove(T)
Removes the first occurrence of the item.
public bool Remove(T item)
Parameters
itemTThe 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
predicatePredicate<T>The condition of the items to remove.
Exceptions
- ArgumentNullException
predicateis null.
RemoveAt(int)
Removes the item at the given index.
public void RemoveAt(int index)
Parameters
indexintThe zero-based index of the item to remove.
Exceptions
- ArgumentOutOfRangeException
indexis 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
indexintThe zero-based index of the first item to remove.
countintThe number of items to remove.
Exceptions
- ArgumentOutOfRangeException
indexorcountis 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
comparerIComparer<T>The comparer to use.
Exceptions
- ArgumentNullException
compareris 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
actionFunc<List<T>, TParam1, TResult>The function that receives the underlying list and
param1.param1TParam1The argument passed to the function, to avoid a capturing closure.
Returns
- TResult
The value returned by the function.
Type Parameters
TParam1The type of the argument passed to the function.
TResultThe type of the result.