Class CopyOnWriteList<T>
A thread-safe variant of
Implements
Namespace: DotNext.Collections.Concurrent
Assembly: DotNext.dll
Syntax
public class CopyOnWriteList<T> : IReadOnlyList<T>, IList<T>, ICloneable
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements held in this collection. |
Remarks
This list is perfect for scenarios when reads are frequent and concurrent but writes not. Read operation never cause synchronization of the list. The enumerator doesn't track additions, removals or changes in the list since enumerator was created. As a result, dirty reads are possible.
Constructors
| Improve this Doc View SourceCopyOnWriteList()
Initializes a new empty list.
Declaration
public CopyOnWriteList()
CopyOnWriteList(IReadOnlyCollection<T>)
Initializes a new list containing elements copied from the given read-only collection.
Declaration
public CopyOnWriteList(IReadOnlyCollection<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyCollection<T> | collection | The source of the items in the creating list. |
Properties
| Improve this Doc View SourceCount
Gets the number of elements in this list.
Declaration
public long Count { get; }
Property Value
| Type | Description |
|---|---|
| Int64 |
Item[Int64]
Gets or sets list item.
Declaration
public T this[long index] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | index | The index of the list item. |
Property Value
| Type | Description |
|---|---|
| T | The list item. |
Snapshot
Gets the snapshot view of the current list.
Declaration
public ReadOnlyMemory<T> Snapshot { get; }
Property Value
| Type | Description |
|---|---|
| ReadOnlyMemory<T> |
Methods
| Improve this Doc View SourceAdd(T)
Adds an item to the end of this list.
Declaration
public void Add(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to be added to the end of this list. null is allowed. |
Remarks
This operation causes reallocation of underlying array.
Clear()
Removes all items from this list.
Declaration
public void Clear()
Clear(Action<T>)
Removes all items from this list and performs cleanup operation for each item.
Declaration
public void Clear(Action<T> cleaner)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | cleaner | The action used to clean item from this list. |
Clear(ValueAction<T>)
Removes all items from this list and performs cleanup operation for each item.
Declaration
public void Clear(in ValueAction<T> cleaner)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueAction<T> | cleaner | The action used to clean item from this list. |
Clone()
Creates copy of this list.
Declaration
public CopyOnWriteList<T> Clone()
Returns
| Type | Description |
|---|---|
| CopyOnWriteList<T> | The copy of this list. |
Contains(T)
Determines whether an item is in this list.
Declaration
public bool Contains(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in this list. |
Returns
| Type | Description |
|---|---|
| Boolean | true, if |
CopyTo(T[], Int32)
Copies the entire list to a compatible one-dimensional array, starting at the specified index of the target array.
Declaration
public void CopyTo(T[] array, int arrayIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The one-dimensional array that is the destination of the elements copied from this list. |
| Int32 | arrayIndex | The zero-based index in |
Exists(Predicate<T>)
Determines whether this list contains elements that match the conditions defined by the specified predicate.
Declaration
public bool Exists(Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| DotNext.Predicate<T> | match | The predicate that defines the conditions of the elements to search for. |
Returns
| Type | Description |
|---|---|
| Boolean | true if array contains one or more elements that match the conditions defined by the specified predicate; otherwise, false. |
Find(Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire list.
Declaration
public T Find(Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| DotNext.Predicate<T> | match | The predicate that defines the conditions of the element to search for. |
Returns
| Type | Description |
|---|---|
| T | The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type |
FindIndex(Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire list
Declaration
public int FindIndex(Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| DotNext.Predicate<T> | match | The predicate that defines the conditions of the element to search for. |
Returns
| Type | Description |
|---|---|
| Int32 | The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1. |
FindLast(Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire list.
Declaration
public T FindLast(Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| DotNext.Predicate<T> | match | The predicate that defines the conditions of the element to search for. |
Returns
| Type | Description |
|---|---|
| T | The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type |
FindLastIndex(Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire list
Declaration
public int FindLastIndex(Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| DotNext.Predicate<T> | match | The predicate that defines the conditions of the element to search for. |
Returns
| Type | Description |
|---|---|
| Int32 | The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1. |
GetEnumerator()
Gets enumerator over snapshot of this list.
Declaration
public CopyOnWriteList<T>.Enumerator GetEnumerator()
Returns
| Type | Description |
|---|---|
| CopyOnWriteList.Enumerator<> | The enumerator over snapshot of this list. |
IndexOf(T)
Returns the zero-based index of the first occurrence of a value in this list.
Declaration
public int IndexOf(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in this list. |
Returns
| Type | Description |
|---|---|
| Int32 | The zero-based index of the first occurrence of |
Insert(Int64, T)
Inserts an element into this list at the specified index.
Declaration
public void Insert(long index, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | index | The zero-based index at which |
| T | item | The object to insert. |
Remarks
This operation causes reallocation of underlying array.
LastIndexOf(T)
Returns the zero-based index of the last occurrence of a value in this list.
Declaration
public int LastIndexOf(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in this list. |
Returns
| Type | Description |
|---|---|
| Int32 | The zero-based index of the last occurrence of |
Remove(T)
Removes the first occurrence of an item from this list.
Declaration
public bool Remove(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to remove from this list. |
Returns
| Type | Description |
|---|---|
| Boolean | true if item is successfully removed; otherwise, false. |
Remarks
This operation causes reallocation of underlying array.
RemoveAll(Predicate<T>)
Removes all the elements that match the conditions defined by the specified predicate.
Declaration
public long RemoveAll(Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| DotNext.Predicate<T> | match | The predicate that defines the conditions of the elements to remove. |
Returns
| Type | Description |
|---|---|
| Int64 | The number of elements removed from this list. |
RemoveAll(Predicate<T>, Action<T>)
Removes all the elements that match the conditions defined by the specified predicate.
Declaration
public void RemoveAll(Predicate<T> match, Action<T> callback)
Parameters
| Type | Name | Description |
|---|---|---|
| DotNext.Predicate<T> | match | The predicate that defines the conditions of the elements to remove. |
| Action<T> | callback | The delegate that is used to accept removed items. |
RemoveAll(ValueFunc<T, Boolean>)
Removes all the elements that match the conditions defined by the specified predicate.
Declaration
public long RemoveAll(ValueFunc<T, bool> match)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueFunc<T, Boolean> | match | The predicate that defines the conditions of the elements to remove. |
Returns
| Type | Description |
|---|---|
| Int64 | The number of elements removed from this list. |
RemoveAll(ValueFunc<T, Boolean>, ValueAction<T>)
Removes all the elements that match the conditions defined by the specified predicate.
Declaration
public void RemoveAll(in ValueFunc<T, bool> match, in ValueAction<T> callback)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueFunc<T, Boolean> | match | The predicate that defines the conditions of the elements to remove. |
| ValueAction<T> | callback | The delegate that is used to accept removed items. |
RemoveAt(Int64)
Removes the element at the specified index of this list.
Declaration
public void RemoveAt(long index)
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | index | The zero-based index of the element to remove. |
Remarks
This operation causes reallocation of underlying array.
Set(ReadOnlySpan<T>)
Replaces all items in this list with given array.
Declaration
public void Set(ReadOnlySpan<T> array)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<T> | array | The array of new items. |
Set<G>(ICollection<G>, Converter<G, T>)
Replaces all items in this list with new items.
Declaration
public void Set<G>(ICollection<G> items, Converter<G, T> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<G> | items | The source items to be converted and placed into this list. |
| DotNext.Converter<G, T> | converter | The convert of source items. |
Type Parameters
| Name | Description |
|---|---|
| G | The type of source items. |
Set<G>(ICollection<G>, ValueFunc<G, T>)
Replaces all items in this list with new items.
Declaration
public void Set<G>(ICollection<G> items, in ValueFunc<G, T> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<G> | items | The source items to be converted and placed into this list. |
| ValueFunc<G, T> | converter | The convert of source items. |
Type Parameters
| Name | Description |
|---|---|
| G | The type of source items. |
Operators
| Improve this Doc View SourceExplicit(CopyOnWriteList<T> to ReadOnlySpan<T>)
Obtains snapshot of the underlying array in the form of the span.
Declaration
public static explicit operator ReadOnlySpan<T>(CopyOnWriteList<T> list)
Parameters
| Type | Name | Description |
|---|---|---|
| CopyOnWriteList<T> | list | The list to be converted into span. |
Returns
| Type | Description |
|---|---|
| ReadOnlySpan<T> |