Class WeakList<T>
Represents a collection of weakly referenced values that maintains relative insertion order and automatically removes values as they are garbage collected.
Namespace: Singulink.Collections
Assembly: Singulink.Collections.Weak.dll
Syntax
public sealed class WeakList<T> : IEnumerable<T>, IEnumerable, IDisposable where T : class
Type Parameters
| Name | Description |
|---|---|
| T | The type of the values in the list. |
Remarks
Values are held through weak references, so adding a value to the list does not prevent it from being garbage collected. When a value is collected its node is removed from the list automatically on the finalizer thread shortly afterwards, so the list never needs to be cleaned and enumeration never returns null values.
Individual operations are safe for concurrent use from multiple threads without external synchronization, including while other threads are enumerating the list. Compound operations that must observe a consistent view of the list still require synchronization, either with UnsafePerformLockedOperation<TState>(TState, Action<WeakList<T>, TState>) or with your own lock. Heavily contended lists may experience reduced performance due to lock contention.
Operation times stated in this documentation assume that no nodes are added by other threads while the operation runs. Operations that enumerate the list visit nodes that other threads add after the current position, so concurrent additions can lengthen them.
Dispose lists that are no longer needed instead of letting them be finalized. The finalizer removes all remaining nodes, which can block the finalizer thread for a significant amount of time if the list is large. A list must not be used after it has been finalized.
Constructors
| Name | Description |
|---|---|
| WeakList() | Initializes a new instance of the WeakList<T> class. |
Properties
| Name | Description |
|---|---|
| Count | Gets the number of nodes in the list. |
| Version | Gets the current version of the list. The version increases every time a node is added, so it can be compared to Version to determine whether a node was added before or after a point in time. |
Methods
| Name | Description |
|---|---|
| AddAfter(Node, T) | Adds a value after the specified node, or near the position the node occupied if it has been removed from the list. |
| AddAfter(Node, T, bool) | Adds a value after the specified node. |
| AddBefore(Node, T) | Adds a value before the specified node, or near the position the node occupied if it has been removed from the list. |
| AddBefore(Node, T, bool) | Adds a value before the specified node. |
| AddFirst(T) | Adds a value to the start of the list. |
| AddLast(T) | Adds a value to the end of the list. |
| Clear() | Removes all nodes from the list. |
| Contains(T, IEqualityComparer<T>?) | Determines whether the list contains a value that is equal to the specified value. |
| Dispose() | Removes all nodes from the list and prevents further use. Most members throw ObjectDisposedException afterwards, and existing enumerators return no further values. |
| ~WeakList() | Finalizes an instance of the WeakList<T> class. |
| Find(Predicate<T>) | Searches for the first value in the list that matches the specified predicate. |
| Find(T, IEqualityComparer<T>?) | Searches for the first value in the list that is equal to the specified value. |
| GetEnumerator() | Returns an enumerator that iterates through the values in the list. |
| GetEnumerator(Node) | Returns an enumerator that iterates through the values in the list starting from the position of the specified node. |
| GetNodeEnumerator() | Returns an enumerator that iterates through the nodes in the list. |
| GetNodeEnumerator(Node) | Returns an enumerator that iterates through the nodes in the list starting from the position of the specified node. |
| InsertAfter(T, T, IEqualityComparer<T>?) | Searches the list for the first value that is equal to |
| InsertBefore(T, T, IEqualityComparer<T>?) | Searches the list for the first value that is equal to |
| Remove(Node) | Removes the specified node from the list. Does nothing if the node has already been removed. |
| Remove(T, IEqualityComparer<T>?) | Removes the first value in the list that is equal to the specified value. |
| TryInsertAfter(T, T, IEqualityComparer<T>?) | Searches the list for the first value that is equal to |
| TryInsertBefore(T, T, IEqualityComparer<T>?) | Searches the list for the first value that is equal to |
| UnsafePerformLockedOperation<TState>(TState, Action<WeakList<T>, TState>) | Runs the specified operation while holding the list's internal lock, so that multiple operations can be performed without other threads modifying the list in between them. |
| UnsafeTryPerformLockedOperation<TState>(TState, Action<WeakList<T>, TState>) | Runs the specified operation while holding the list's internal lock if the lock can be acquired immediately, so that multiple operations can be performed without other threads modifying the list in between them. |
Explicit Interface Implementations
| Name | Description |
|---|---|
| IEnumerable<T>.GetEnumerator() | Returns an enumerator that iterates through the collection. |
| IEnumerable.GetEnumerator() | Returns an enumerator that iterates through a collection. |